简体   繁体   English

如何在C#中为多个按钮重复使用OpenFileDialog方法

[英]How to re-use OpenFileDialog method in C# for multiple buttons

Currently I have this code: 目前,我有以下代码:

 private void FirstButton_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Reset();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
              //some code
            }

        }

and same code for 和相同的代码

private void SecondButton_Click(object sender, EventArgs e)

    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Reset();
        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
          //some code
        }

    }

I realized that I need to reuse the OpenFileDialog multiple times, so I'm asking how can I make it to be called multiple times? 我意识到我需要多次重用OpenFileDialog,所以我问如何才能多次调用它?

I already look into these: link1 , link2 我已经研究了这些: link1link2

but to no avail. 但无济于事。 Thank you in advance. 先感谢您。

You need to declare the OpenFileDialog outside of either method. 您需要任一方法之外声明OpenFileDialog

For example, you could make it a field: 例如,您可以将其设置为字段:

protected OpenFileDialog _openFileDialog = new OpenFileDialog();

Then both methods can use it: 然后两种方法都可以使用它:

// Call this from Form_Load() or some such:
private void InitializeOpenFileDialog()
{
    _openFileDialog.InitialDirectory = "c:\\";
    _openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    _openFileDialog.FilterIndex = 2;
    _openFileDialog.RestoreDirectory = true;
}

private void FirstButton_Click(object sender, EventArgs e)
{
    // not needed now: OpenFileDialog openFileDialog1 = new OpenFileDialog();
    _openFileDialog.Reset();
    if (_openFileDialog.ShowDialog() == DialogResult.OK)
    {
      //some code
    }
}

private void SecondButton_Click(object sender, EventArgs e)
{
    // not needed now: OpenFileDialog openFileDialog1 = new OpenFileDialog();
    _openFileDialog.Reset();
    if (_openFileDialog.ShowDialog() == DialogResult.OK)
    {
      //some code
    }
}

This is what I meant with my comment: 这就是我对评论的意思:

using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private System.Windows.Forms.DialogResult dialogFunction()
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Reset();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            return (openFileDialog1.ShowDialog());

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dialogFunction() == System.Windows.Forms.DialogResult.OK)
            {
             /*do stuff*/   
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dialogFunction() == System.Windows.Forms.DialogResult.OK)
            {
                /*do stuff*/
            }

        }


    }
}

And actually you could just use the very same handler for both clicks 实际上,您可以为两次点击使用完全相同的处理程序

 private void OneClickForAll(object sender, EventArgs e)
        {
            if (dialogFunction() == System.Windows.Forms.DialogResult.OK)
            {
                /*do stuff*/
            }

        }

go to properties of each button 转到每个按钮的属性

在此处输入图片说明

and in the events tab 并在事件标签中

在此处输入图片说明

Select the function above (you could even just delete one of the button_Click functions and assign the other if both buttons are doing the very same. 选择上面的功能(如果两个按钮的功能相同,您甚至可以删除其中一个button_Click功能,然后分配另一个功能。

在此处输入图片说明

So now you will have something like 所以现在你会有类似的东西

private System.Windows.Forms.DialogResult dialogFunction()
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Reset();
    openFileDialog1.InitialDirectory = "c:\\";
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;
    return (openFileDialog1.ShowDialog());

}
private void OneClickForAll(object sender, EventArgs e)
{
    if (dialogFunction() == System.Windows.Forms.DialogResult.OK)
    {
        /*do stuff*/
    }

}

If you need to do diff stuff on each button if dialogResult == "OK" you could use the name property of every button and use a switch sentence: 如果在dialogResult ==“ OK”的情况下,如果需要对每个按钮进行差异处理,则可以使用每个按钮的name属性,并使用切换语句:

在此处输入图片说明

if (dialogFunction() == System.Windows.Forms.DialogResult.OK)
{
    switch (((Button)sender).Name)
    {
        case "button1":
                    /*do stuff for button 1 click*/
                    MessageBox.Show("you clicked button 1");
        break;
        case "button2":
                    /*do stuff for button 1 click*/
                    MessageBox.Show("you clicked button 2");
        break;
        default:                        
        break;        
    }
}

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

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