2013-05-11 52 views
4

此應用程序提示您打開一個文件夾。該應用程序然後查看文件夾中的所有文件併爲每個(.wav)文件生成一個按鈕。然後,我打算在按下按鈕時播放(.wav)文件。是否有可能有多個控制按鈕。標籤

因爲它是我動態創建按鈕。我使用button.Tag發送按鈕號碼,但是我希望發送另一個保存wav文件完整路徑的對象。然而,我已經僞加了它,我知道你不能像我所做的那樣添加兩個button.Tag。所以我的問題是如何實現這一點。

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(); 
    } 


} 
+1

聲明一個類** ExtraButtonInfo **,它包含** all **每個按鈕所需的額外信息,並將** ExtraButtonInfo **的實例分配給每個Button的Tag屬性。 – 2013-05-11 15:55:41

回答

3

我推薦一個專門的對象在Tag屬性來存儲:

class WaveDetailTag { 

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

} 

實現:

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

更新
使用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; 
     } 
    } 
+0

+1更多關於如何做到這一點,請參閱我的評論(稍微)。 – 2013-05-11 15:56:26

+0

@IAbstract我將如何去在開關櫃中使用它? – 2013-05-11 16:13:28

+0

非常感謝,這個作品很棒! – 2013-05-11 16:33:06

0

多個標籤是不是真的有可能,但你可以使用一個ListList<object>List<MyType>)或數組作爲Tag

你也可以創建自己的類包含所有想要的信息,這將是可取的:

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

與用法:

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

感謝您的輸入! – 2013-05-11 16:34:44

0

您可以將列表存儲到標籤屬性,允許您在其中存儲多個對象。

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

未知的方法或擴展名爲'Add'的類型爲'object'的'Tag',或類似的東西。您應該將'Tag'投射到'List '以便將其用作列表。 – SimpleVar 2013-05-11 15:56:17

+0

標記是一個「對象」,並且必須在添加任何東西之前進行投射 – IAbstract 2013-05-11 15:56:30

+0

感謝您的提示。 – 2013-05-11 15:57:18

0

使用自定義類並將其存儲在Tag財產,是這樣的:

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

..... 


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

感謝您的輸入! – 2013-05-11 16:34:21

1

你應該設置你標記像

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

的,當你想找回你的價值觀,你可以使用動態

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

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


你還可以創建第二個解決方案的新方法

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

調用簡單地使用

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

感謝您的輸入! – 2013-05-11 16:35:06

+1

謝謝4你的幫助。 – 2017-10-25 17:37:43

0

我知道爲時已晚,但我想出一個簡單的方法來做到這一點。 剛剛加入像string join = String.join(","firstString,secondString); button.Tag = join; 字符串,並在使用前,它只是它分成兩個字符串,這樣

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

和使用字符串的標題和鏈接字符串,只要你喜歡。 (Y) 我不知道這是否是一種好的做法,但我正在使用它,它的工作完美。

我是新來的c#。

相關問題