Is it possible to have several control buttons.

This application will prompt you to open a folder. Then the application scans all the files in the folder and generates a button for each (.wav) file. My intention was to play the file (.wav) at the click of a button.

Be that as it may, I am dynamically creating buttons. I use button.Tag to send the button number, however I want to send another object that contains the full path to the wav file. However, I added it, but I know that you cannot add two button.Tag , as I did. So my question is how to 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(); } } 
+4
source share
6 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
Usage 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; } } 
+3
source

you must set the tag as

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

when you want to get 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(); 

<h / "> you can also create a new method for the second solution

 object GetValueByProperty(object obj,string propName) { return obj.GetType().GetProperty(propName).GetValue(obj, null); } 

to call just use

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

Multiple tags are NOT REALLY possible, but you can use List ( List<object> or List<MyType> ) or an array like Tag .

You can also create your own class to contain all the necessary information, which would be preferable:

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

And use:

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

You can save the List property in a tag, which allows you to store several objects in it.

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

Use your own class and save it in the Tag property, for example:

 public class WaveButton { public int Number { get; set; } public string WaveFilePath { get; set; } } ..... button.Tag = new WaveButton() { Number = n, WaveFilePath = path } 
0
source

I know him too late, but I came up with an easy way to do this. just string join = String.join(","firstString,secondString); button.Tag = join; strings like string join = String.join(","firstString,secondString); button.Tag = join; string join = String.join(","firstString,secondString); button.Tag = join; and before using it, just divide it into two lines, such as

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

and use the row header and row as you like. (Y) I donโ€™t know if this is good practice or not, but I use it and it works perfectly.

I am new to C #.

0
source

Source: https://habr.com/ru/post/1480259/


All Articles