简体   繁体   中英

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?

I already look into these: link1 , link2

but to no avail. Thank you in advance.

You need to declare the OpenFileDialog outside of either method.

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.

在此处输入图片说明

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:

在此处输入图片说明

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;        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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