【问题标题】:passing value to handler将值传递给处理程序
【发布时间】:2016-06-28 11:04:01
【问题描述】:

我有这样的代码:

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,所以我只是切换到那里并执行操作。

问题是我不知道如何将它传递给处理程序,如果可能的话。另外如果有其他解决办法,请写出来。

【问题讨论】:

  • 您想在哪里使用文本框中的文本?
  • @MongZhu 无处,只是为了展示
  • 好的,那么你想在哪里显示呢?在同一个Form 中?或在OpenfileDialog 中?
  • @sowjanyaattaluri 我知道,请再次阅读我的问题。我知道如何使用相同的处理程序,但我需要处理程序的操作依赖于调用它的按钮
  • @MongZhu同格式

标签: c# winforms fileopendialog


【解决方案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
        }
    }

【讨论】:

  • 但是如果我这样做,如果扩展名不是.fdb,我无法取消事件
  • 你总是可以把你的检查放在代码之上,伙计。就这么简单,只需一个简单的return,您就可以告诉您的代码在您的第一次检查未完成时不要进一步执行。
【解决方案2】:

您可以使用一个私有字段来临时保存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)
    {
         ....
    }


}

【讨论】:

  • 是的,这是我想到的解决方案之一,但我认为有更有效的方法来代替声明全局变量。还是谢谢。
  • @Pacijent 我很确定有。只是迟到了一秒。你还可以看看。
  • 我刚刚再次阅读了您的问题,我的解决方案似乎不太正确。您不想将结果值从OpenFileDialog 传递给相应的TextBoxes 吗?
  • @Pacijent 我编辑了我的答案。现在是不是更接近你的想法了?就个人而言,我喜欢 D. Petrov 建议的答案。
  • 是的,我从一开始就是这样做的。稍微更改了您的代码,它工作正常。 D. Petrov 也是很好的解决方案。目前我正在做另一个解决方案,因为我现在知道这样做的逻辑,所以我会在完成后发布。
猜你喜欢
  • 2014-08-29
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 2023-03-18
相关资源
最近更新 更多