简体   繁体   English

是否可以有多个控制按钮。标签

[英]Is it possible to have multiple control button.Tag

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. 然后,应用程序查看文件夹中的所有文件,并为每个(.wav)文件生成一个按钮。 My intention was then to play the (.wav) file when the button is pressed. 我的意图是按下按钮时播放(.wav)文件。

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. 我使用button.Tag发送按钮编号,但我希望发送另一个保存wav文件完整路径的对象。 I have pseudo added it however, I know you can not add two button.Tag like I have done. 我有伪添加它然而,我知道你不能添加两个button.Tag像我做的那样。 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();
    }


}

I recommend a specialized object to store in the Tag property: 我建议在Tag属性中存储一个专门的对象:

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 : 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;
        }
    }

you should set you tag like 你应该为你设置标签

button.Tag= new {Count = btnCount, FileName=soundFfilepath);//anonymouse type

the when you want to retrieve your values you can use dynamic 当您想要检索您的值时,您可以使用动态

int count=((dynamic)button.Tag).Count;
string filename=((dynamic)button.Tag).FileName;

or 要么

int count=(int) button.Tag.GetType().GetProperty("Count").GetValue(obj, null);
string filename= button.Tag.GetType().GetProperty("FileName").GetValue(obj, null).ToString();


you can also create a new method for the second solution 您还可以为第二个解决方案创建新方法

int count=(int) GetValueByProperty(button.Tag,"Count");
string filename=GetValueByProperty(button.Tag,"FileName");.ToString();

to call simply use 打电话只是用

 int count=(int) GetValueByProperty(button.Tag,"Count"); string filename=GetValueByProperty(button.Tag,"FileName");.ToString(); 

Multiple tags isn't REALLY possible, but you can use a List ( List<object> or List<MyType> ) or an array as the Tag . 多个标签不可能,但您可以使用ListList<object>List<MyType> )或数组作为Tag

You can also create your own class to contain all wanted information, which would be preferable: 您还可以创建自己的类以包含所有想要的信息,这将是更好的:

class MyTagInfo
{
    public int Num;
    public string Text;
    public bool SomeMoreInfo;
}

And usage: 用法:

myControl.Tag = new MyTagInfo { Num = 0, Text = "Hello World!" };

You can store a List into the Tag property, which allows you to store multiple objects in it. 您可以将List存储到Tag属性中,该属性允许您在其中存储多个对象。

button.Tag = new List<object>();
(List<object> button.Tag).Add(btnCount);
(List<object> button.Tag).Add(soundfilepath);

Use a custom class and store it in the Tag property, something like: 使用自定义类并将其存储在Tag属性中,例如:

public class WaveButton
{
    public int Number { get; set; }
    public string WaveFilePath { get; set; }
}

.....


button.Tag = new WaveButton() { Number = n, WaveFilePath = path }

I know its too Late but i come up with an easy way to do that. 我知道它太晚了,但我想出了一个简单的方法来做到这一点。 just join strings like string join = String.join(","firstString,secondString); button.Tag = join; 只需加入string join = String.join(","firstString,secondString); button.Tag = join; string join = String.join(","firstString,secondString); button.Tag = join; and before using it just split it into two strings like this 在使用它之前,只需将它分成两个字符串即可

string[] split = join.Split(',');
        string title = split[1];
        string link = split[0];

and use string title and string link as you like. 并根据需要使用字符串标题和字符串链接。 (Y) I don't know if it is a good practice or not but i am using it and it work perfect. (Y)我不知道这是不是一个好习惯,但我正在使用它并且它完美无缺。

I am new to c#. 我是c#的新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM