2016-06-28 46 views
-1

我有这样的代码:传递值到处理器

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目录传递给不同的文本框。因此,逻辑是这样的:

如果我和按钮1,通价值TextBox1中, 如果我BUTTON2开开,传递价值TextBox2中, 如果我BUTTON3打开,传递价值textbox3。所以我想创建int检查(1,2,3),所以当我按下button1时,它将check = 1传递给OpenDialog1_FileOk,所以我只是在那里切换并执行操作。

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

+0

你想在哪里使用文本框? –

+0

@MongZhu不通,只要显示 – Pacijent

+0

好,那你想在哪里显示呢?在同一个“形式”?或在'OpenfileDialog'中? –

回答

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

但是,如果我这样做,我不能取消事件,如果扩展名不是.fdb – Pacijent

+0

你可以随时把你的检查上面的代码,男人。这很简单,只需要一个简单的'return',你可以告诉你的代码在第一次检查没有完成时不能进一步执行。 –

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) 
    { 
     .... 
    } 


} 
+0

是的,这是我想到的解决方案之一,但我认为有更高效的方式做到这一点而不是声明全局变量。不管怎么说,还是要谢谢你。 – Pacijent

+0

@Pacijent我很确定有。迟了一秒。你仍然可以看看。 –

+0

我刚刚再次阅读您的问题,我的解决方案似乎不太正确。你不想将结果值从'OpenFileDialog'传递给相应的'TextBoxes'? –