简体   繁体   English

C#通过单击按钮调用文件?

[英]C# Call a file from a button click?

I have a Form and a Class: Form1 & Timer 我有一个Form和一个Class: Form1Timer

In Form1 I have a button that looks like this 在Form1中,我有一个看起来像这样的按钮

public void browseSoundToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog1 = new OpenFileDialog();

    dialog1.Title = "Browse to find sound file to play";
    dialog1.InitialDirectory = @"c:\";
    dialog1.Filter = "Wav Files (*.wav)|*.wav";
    dialog1.FilterIndex = 2;
    dialog1.RestoreDirectory = true;

    //PlaySound(dialog1.FileName, new System.IntPtr(), PlaySoundFlags.SND_SYNC);             
}

In Timer.cs I have an Event that looks like this: 在Timer.cs中,我有一个如下所示的Event

public void alert_sound(object source, ElapsedEventArgs e)
{
    MessageBox.Show("Alert Sound ding ding ding");

    //PlaySound(dialog1.FileName, new System.IntPtr(), Form1.PlaySoundFlags.SND_SYNC);
    alert_timer.Stop();
}

The problem is that in Timer.cs it says that dialog1 does not exist in the current context. 问题是在Timer.cs中它说dialog1在当前上下文中不存在。 How do I call dialog 1 from Form1 into my Timer class? 如何将对话框1从Form1调用到Timer类中?

You have created the dialog1 variable in a scope local to the browseSoundToolStripMenuItem_Click event method. 您已经在browserSoundToolStripMenuItem_Click事件方法的本地作用域中创建了dialog1变量。 In order to access it from a different event you need to create it at the class level. 为了从其他事件访问它,您需要在类级别创建它。 Like this: 像这样:

public partial class Form1 : Form
{
    OpenFileDialog dialog1 = new OpenFileDialog();


    public void browseSoundToolStripMenuItem_Click(object sender, EventArgs e)
    {
        dialog1.Title = "Browse to find sound file to play";
        dialog1.InitialDirectory = @"c:\";
        dialog1.Filter = "Wav Files (*.wav)|*.wav";
        dialog1.FilterIndex = 2;
        dialog1.RestoreDirectory = true;

        //PlaySound(dialog1.FileName, new System.IntPtr(), PlaySoundFlags.SND_SYNC);             
    }

    public void alert_sound(object source, ElapsedEventArgs e)
    {
        MessageBox.Show("Alert Sound ding ding ding");

        //PlaySound(dialog1.FileName, new System.IntPtr(), Form1.PlaySoundFlags.SND_SYNC);
    alert_timer.Stop();
    }

}

In your example, dialog1 needs to be declared at the module level. 在您的示例中,dialog1需要在模块级别声明。 You declared it within the browseSoundToolStripMenuItem_Click() method, so dialog1 only has scope for that method. 您是在browserSoundToolStripMenuItem_Click()方法中声明的,因此dialog1仅具有该方法的作用域。

Move this line: 移动这一行:

OpenFileDialog dialog1 = new OpenFileDialog();

to the top of the form. 到表格的顶部。

your going to have to store the filename in a variable that can be called by the other method. 您将必须将文件名存储在可以由其他方法调用的变量中。

string filename = dialog1.Filename;

Possible to use the filename variable as a class level variable, but that is a suggestion as I don't know anything about your class and its layout. 可以将filename变量用作类级别的变量,但这是一个建议,因为我对您的类及其布局一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM