将值传递给处理程序

时间:2016-06-28 11:04:02

标签: c# winforms fileopendialog

我有这样的代码:

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

现在,我想创建更多打开同一文件对话框的按钮。问题是我想将openFileDialog目录传递给不同的文本框。所以逻辑就是这样:

如果我用button1打开,将值传递给textbox1, 如果我用button2打开,将值传递给textbox2, 如果我用button3打开,将值传递给textbox3。

所以我想创建int check(1,2,3)所以当我按下button1时,它会将check = 1传递给OpenDialog1_FileOk,所以我只是在那里切换并执行操作。

问题是我不知道如何将它传递给处理程序,如果可能的话。如果还有其他解决方案,请写下来。

2 个答案:

答案 0 :(得分:1)

首先,您可以像这样使用openfiledialog,而无需处理全新的功能:

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

其次,为了您的目标,您必须确保控件的名称正好以您想要的数字结尾(例如“button1”和“textbox1”)。然后你就可以这样做:

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

答案 1 :(得分:0)

您可以使用私人字段暂时保存TextBox的文本,并将其部署在点击事件中,如下所示:

private int whichButton = 0;

private void button1_Click(object sender, EventArgs e)
{
    whichButton = 1;
    openFileDialog1.ShowDialog();
}


private void button2_Click(object sender, EventArgs e)
{
    whichButton = 2;
    openFileDialog1.ShowDialog();
}


private void button3_Click(object sender, EventArgs e)
{
    whichButton = 3;
    openFileDialog1.ShowDialog();
}

然后使用whichButton选择

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{ 
    switch (whichButton)
    {
         ....
    }


}
相关问题