[英]C# apply a different file to the same variable
I am trying to create a series of buttons, each play a sound. 我正在尝试创建一系列按钮,每个按钮都发出声音。 This sound is retrieved from an OpenFileDialog function.
从OpenFileDialog函数检索此声音。 However, I have encountered the issue of one sound being assigned to all of the buttons.
但是,我遇到了一种声音分配给所有按钮的问题。 I know why this occurring, but I am unsure of how to resolve the issue.
我知道为什么会这样,但是我不确定如何解决该问题。 Basically, I began by assigning the same algorithms to each button:
基本上,我首先为每个按钮分配了相同的算法:
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
}
And: 和:
soundPlayer = new SoundPlayer(fileName);
soundPlayer.Play();
Unfortunately, this was extremely ugly and so I decided to put each algorithm in to a method and just call the methods to their respective buttons. 不幸的是,这非常丑陋,所以我决定将每种算法都放在一个方法中,然后只将方法调用到它们各自的按钮上。 Like so:
像这样:
public void openDialog()
{
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
}
}
private void button27_Click(object sender, EventArgs e)
{
openDialog();
}
public void playDialog()
{
soundPlayer = new SoundPlayer(fileName);
soundPlayer.Play();
}
private void button1_Click(object sender, EventArgs e)
{
playDialog();
}
However, because openDialog()
calls the same variable which receives the file name, each of the buttons calling openDialog()
is using the same variable and so playing the same sound. 但是,由于
openDialog()
调用了接收文件名的相同变量,因此调用openDialog()
每个按钮都使用相同的变量,因此播放相同的声音。
You have to make the fileName "part" of the Button. 您必须将fileName设置为Button的“一部分”。 You can do it by either:
您可以通过以下两种方式之一进行操作:
Make a pick. 选一个。
For example, using a Tag: 例如,使用标签:
public void playDialog(string fileName)
{
soundPlayer = new SoundPlayer(fileName);
soundPlayer.Play();
}
private void button1_Click(object sender, EventArgs e)
{
playDialog((sender as Button).Tag as string);
}
You can make a list of sounds and then play it in a loop one by one: 您可以制作声音列表,然后一个接一个地循环播放:
Creating the list: 创建列表:
List<string> soundsList = new List<string>();
Adding to the list: 添加到列表:
sounds.Add(openFileDialog.FileName);
Playing sounds: 播放声音:
foreach(string sound in soundsList)
{
soundPlayer = new SoundPlayer(sound);
soundPlayer.Play();
}
My answer of course is assuming you keep an order of first adding all the sounds you want and then playing them all. 我的答案当然是假设您保持先添加所有想要的声音然后全部播放的命令。 You should of course also need to add validation to check that the user has given you a correct sound to add to the list.
当然,您还应该添加验证,以检查用户是否给了您正确的声音以添加到列表中。
EDIT: After reading your comment, you can also add a sound to Tag
property of the button. 编辑:阅读评论后,您还可以向按钮的
Tag
属性添加声音。 Then when you want to play a sound of a specific button, you can just play whatever is inside that property of the button. 然后,当您想播放特定按钮的声音时,您可以播放按钮的该属性内的任何内容。
For example you can override the Click event like this: 例如,您可以像这样覆盖Click事件:
private void button_Click(object sender, EventArgs e)
{
string soundFile = (sender as Button).Tag as string;
playDialog(soundFile);
}
This way all sounds are a "part" of the button 这样,所有声音都是按钮的“一部分”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.