2013-04-11 45 views
1

当选择一个.dll文件并按OK时,浏览对话框关闭并重新打开。在它再次打开并按下确定后,它将采取没有任何问题的值。'OpenFileDialog'在第一次没有取值

这是我的代码

private void btnBrowse_Click(object sender, EventArgs e) { 
      OpenFileDialog dllDialog = new OpenFileDialog(); 
      dllDialog.Filter = "DLL Files|*.dll"; 
      dllDialog.InitialDirectory = @"C:\"; 
      dllDialog.Title = "Please select .dll file."; 
      if (dllDialog.ShowDialog() == DialogResult.OK) { 
       dllDialog.ShowDialog(); 
       tbRepTempLibrary.Text = dllDialog.FileName; 

      } else { 
       MessageBox.Show("error"); 
      } 
     } 
+0

你可以调用'ShowDialog()'两次 - 你期望会发生什么? – 2013-04-11 09:04:45

+0

您是否担心第一次只显示FileName并且第二次显示FullFileName(包括路径)? – Jehof 2013-04-11 09:06:36

回答

2

您呼叫ShowDialog()两次。您应该删除第二个,

private void btnBrowse_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog dllDialog = new OpenFileDialog(); 
    dllDialog.Filter = "DLL Files|*.dll"; 
    dllDialog.InitialDirectory = @"C:\"; 
    dllDialog.Title = "Please select .dll file."; 
    if (dllDialog.ShowDialog() == DialogResult.OK) 
    { 
     tbRepTempLibrary.Text = dllDialog.FileName; 

    } else 
    { 
     MessageBox.Show("error"); 
    } 
} 
+0

它现在工作正常,谢谢 – IBRA 2013-04-11 09:07:50

+1

不客气'')' – 2013-04-11 09:12:36

2

你不应该叫dllDialog.ShowDialog()两次,用这个来代替:

if (dllDialog.ShowDialog() == DialogResult.OK) 
{ 
    tbRepTempLibrary.Text = dllDialog.FileName; 
} 

如果用户点击取消,因为不希望选择一个文件,你不应该显示error,这是用户取消而不打开或选择文件的权利,您完全不会继续操作开始;-)

+0

谢谢,错误信息只是为了测试目的:) – IBRA 2013-04-11 09:12:12

2

你打了两次电话ShowDialog()。您应该只是这样做:

private void btnBrowse_Click(object sender, EventArgs e) { 
     OpenFileDialog dllDialog = new OpenFileDialog(); 
     dllDialog.Filter = "DLL Files|*.dll"; 
     dllDialog.InitialDirectory = @"C:\"; 
     dllDialog.Title = "Please select .dll file."; 
     if (dllDialog.ShowDialog() == DialogResult.OK) { 
      tbRepTempLibrary.Text = dllDialog.FileName; 

     } else { 
      MessageBox.Show("error"); 
     } 
    } 
2
if (dllDialog.ShowDialog() == DialogResult.OK) { 
      dllDialog.ShowDialog(); // This shouldn't be here 

您显示对话框的两倍。