Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have multiple control button.Tag

Tags:

c#

winforms

This app prompts you to open a folder. The app then looks at all the files in the folder and generates a button for each (.wav) file. My intention was then to play the (.wav) file when the button is pressed.

As it is I am dynamically creating the buttons. I use the button.Tag to send the button number however I wish to send another object that holds the full path to the wav file. I have pseudo added it however, I know you can not add two button.Tag like I have done. So my question is how do I implement this.

public partial class Form1 : Form
{
    public SoundPlayer Sound1;
    public static int btnCount = 0;

    public Form1()
    {
        InitializeComponent();
        SetFolderPath();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void addDynamicButton(string folder, string fileName)
    {
        btnCount++;
        string soundfilepath = folder + "\\" + fileName + ".wav";

        Button button = new Button();
        button.Location = new Point(20, 30 * btnCount + 10);
        button.Size = new Size(300, 23);

        button.Text = fileName;
        button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        button.UseVisualStyleBackColor = true;

        button.Click += new EventHandler(btnDynClickEvent);
        button.Tag = btnCount;
        button.Tag = soundfilepath;
        this.Controls.Add(button);

    }

    void btnDynClickEvent(object sender, EventArgs e)
    {
        Button button = sender as Button;
        if (button != null)
        {

            switch ((int)button.Tag)
            {
                case 1:
                    Sound1 = new SoundPlayer((string)button.Tag);
                Sound1.Play();
                break;

            }
        }
    }

    public void SetFolderPath()
    {

        FolderBrowserDialog folder = new FolderBrowserDialog();

        folder.Description = "Select the sound file Folder";

        if (textBox1.Text.Length > 2)
        {
            folder.SelectedPath = textBox1.Text;
        }

        else
        {
            folder.SelectedPath = @"C:\";
        }

        if (folder.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = folder.SelectedPath;

            string[] files = Directory.GetFiles(folder.SelectedPath, "*.wav", SearchOption.AllDirectories);
            int count = files.Length;

            richTextBox1.Text = count.ToString() + " Files Found";

            foreach (string file in files)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);
                addDynamicButton(folder.SelectedPath, fileName);
            }
        }
    }


    private void btnOpenFolder(object sender, EventArgs e)
    {
        SetFolderPath();
    }


}
like image 462
Ryan Walkowski Avatar asked May 11 '13 15:05

Ryan Walkowski


1 Answers

I recommend a specialized object to store in the Tag property:

class WaveDetailTag {

    public FileInfo WaveFile {get; set;}
    public int ButtonId {get; set;}

}

Implementation:

// ...
button.Tag = new WaveDetailTag() { 
    WaveFile = new FileInfo(soundfilepath), 
    ButtonId = btnCount 
};
// ...

Update
Using in switch-case:

    Button button = sender as Button;
    if (button != null)
    {
        var waveDetail = (WaveDetailTag)button.Tag;
        switch (waveDetail.ButtonId)
        {
            case 1:
                Sound1 = new SoundPlayer(waveDetail.WaveFile.FullName);
                Sound1.Play();
                break;
        }
    }
like image 185
IAbstract Avatar answered Sep 19 '22 11:09

IAbstract