Search code examples
.netopenfiledialog

Reusing OpenFileDialog


I have 2 textboxes and 2 button [...] next to each textbox. Is it possible to use one OpenFileDialog and pass the FilePath to the respective textbox, based on which button is clicked? i.e...if I click buttton one and laod the dialog, when I click open on the dialog, it passes the fileName to the first textbox.


Solution

  • This worked for me (and it's simpler than the other posts, but either of them would work as well)

    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        textBox1.Text = openFileDialog1.FileName;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        textBox2.Text = openFileDialog1.FileName;
    }