Search code examples
c#winformsfileopendialog

passing value to handler


I have code like this:

private void button1_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{ 
    string ext = Path.GetExtension(openFileDialog1.FileName);
    if(string.Compare(ext, ".FDB") == 0)
    {
        string fileName = openFileDialog1.SafeFileName;
        string fileDirectory = Path.GetDirectoryName(openFileDialog1.FileName);
        string databaseTxt = @"C:\Users\arist\AppData\Roaming\TDWork\";
        string[] database = { fileDirectory + fileName };
        if (Directory.Exists(databaseTxt))
        {
            System.IO.File.WriteAllLines(databaseTxt + "databases.txt", database);
        }
        else
        {
            DirectoryInfo di = Directory.CreateDirectory(databaseTxt);
            System.IO.File.WriteAllLines(databaseTxt + "databases.txt", database);
        }
    }
    else
    {
        MessageBox.Show("Fajl koji ste izabrali nije Firebird baza (.FDB)");
        e.Cancel = true;
    }                        
}

Now, i want to create more buttons that open same file dialog. Problem is that i want to pass openFileDialog directory to different textboxes. So logic is this:

If i open with button1, pass value to textbox1, If i open with button2, pass value to textbox2, if i open with button3, pass value to textbox3.

So i wanted to create int check (1, 2, 3) so when i press button1, it pass check = 1 to OpenDialog1_FileOk, so i just do switch there and do actions.

Problem is i do not know how to pass it to handler, and if that is possible. Also if there is any other solution, please write it.


Solution

  • First, you could use your openfiledialog just like this, without handling a whole new function for it:

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

    Second, for your goal you'll have to be sure that the names of your controls are exactly ending on the digit you want (e.g. "button1" and "textbox1"). Then you can do it like this:

    void Button1Click(object sender, EventArgs e)
        {
    
            //MessageBox.Show(bt.Name[bt.Name.Length - 1].ToString());
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
    
                if(!Path.GetExtension(openFileDialog1.FileName).EndsWith(".FDB"))  //checking if the extension is .FDB (as you've shown in your example)
                {   
                    MessageBox.Show("Fajl koji ste izabrali nije Firebird baza (.FDB)");
                    return; //return if it's not and no further code gets executed
                }
    
                string fileDirectory = Path.GetDirectoryName(openFileDialog1.FileName); //getting the directory
                string nameOfMyButton = (sender as Button).Name;    //you get the name of your button
                int lastDigitOfMyName = Convert.ToInt16(Name[Name.Length - 1]); //returns the number of your button
                TextBox neededTextboxToShowDirectory = this.Controls.Find("textbox" + lastDigitOfMyName, true).FirstOrDefault() as TextBox; //this will search for a control with the name "textbox1"
                neededTextboxToShowDirectory.Text = fileDirectory; //you display the text
                //... doing the rest of your stuff here
            }
        }