此應用程序提示您打開一個文件夾。該應用程序然後查看文件夾中的所有文件併爲每個(.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();
}
}
聲明一個類** ExtraButtonInfo **,它包含** all **每個按鈕所需的額外信息,並將** ExtraButtonInfo **的實例分配給每個Button的Tag屬性。 – 2013-05-11 15:55:41