Thursday, February 10, 2011

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.

  • There are several ways to do this. One is to have a Dictionary<Button, TextBox> that holds the link between a button and its related textbox, and use that in the click event for the button (both buttons can be hooked up to the same event handler):

    public partial class TheForm : Form
    {
        private Dictionary<Button, TextBox> _buttonToTextBox = new Dictionary<Button, TextBox>();
        public Form1()
        {
            InitializeComponent();
            _buttonToTextBox.Add(button1, textBox1);
            _buttonToTextBox.Add(button2, textBox2);
        }
    
        private void Button_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                _buttonToTextBox[sender as Button].Text = ofd.FileName;
            }
        }
    }
    

    Of course, the above code should be decorated with null-checks, nice encapsulation of behavior and so on, but you get the idea.

  • Yes it is, basically you need to keep a reference to the button that was clicked, and then a mapping of a textbox to each button:

    public class MyClass
    {
      public Button ClickedButtonState { get; set; }
      public Dictionary<Button, TextBox> ButtonMapping { get; set; }
    
      public MyClass
      {
        // setup textbox/button mapping.
      } 
    
       void button1_click(object sender, MouseEventArgs e)
       {
         ClickedButtonState = (Button)sender;
         openDialog();
       }
    
       void openDialog()
       {
         TextBox current = buttonMapping[ClickedButtonState];
         // Open dialog here with current button and textbox context.
       }
    }
    
    From Russell
  • 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;
    }
    
  • Whenever you think "there common functionality!" you should consider a method to implement it. It could look like this:

        private void openFile(TextBox box) {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK) {
                box.Text = openFileDialog1.FileName;
                box.Focus();
            }
            else {
                box.Text = "";
            }
        }
    
        private void button1_Click(object sender, EventArgs e) {
            openFile(textBox1);
        }
    

0 comments:

Post a Comment