简体   繁体   English

C#将不同的文件应用于相同的变量

[英]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: 您可以通过以下两种方式之一进行操作:

  • Using the Tag property of a button and cast to string when retrieving 使用按钮的Tag属性并在检索时强制转换为字符串
  • Create a subclass of a Button called SoundButton and add FileName property of type string 创建一个名为SoundButton的Button的子类,并添加string类型的FileName属性

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.

相关问题 C#将文件重命名为与其他目录中的文件相同的名称 - C# rename a file to the same name as a file from a different directory 用C#中的不同输入多次替换文件中的相同字符串 - Replacing the same string multiple times in a file with different input in C# C#对所有文本框应用相同的规则 - c# apply same rules to all textboxes 使用不同的脚本文件Unity C#将数据保存到变量 - Saving Data to Variable With Different Script File Unity C# C#:以编程方式将合并/修补程序应用于文件? - C#: Programmatically apply merge/patch to file? 使用protobuf-net为c#生成的文件与在C ++中生成的同一文件略有不同 - Generated file with protobuf-net for c# is different a little from the same file generated in C++ c# 和 c 的相同程序不同 output - Same program different output for c# and c 我有 3 个不同的类,具有相同的方法名称。 需要在使用前检查并应用它们的正确实例c# - I have 3 different classes, with the same method names. Need to check before usage and apply the right instance of them c# 相同的变量名称 - 2个不同的类 - 如何将值从一个复制到另一个 - 反射 - C# - Same Variable Names - 2 Different Classes - How To Copy Values From One To Another - Reflection - C# 为什么C#不允许我在不同范围内使用相同的变量名? - Why doesn't C# allow me to use the same variable name in different scopes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM