1

选择 .dll 文件并按 OK 后,浏览对话框将关闭并重新打开。在它再次打开并按 OK 后,它会毫无问题地取值。

这是我的代码

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");
            }
        }
4

4 回答 4

2

您不应该调用dllDialog.ShowDialog()两次,而是使用它:

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

如果用户因为不想选择您不应该显示的文件而单击取消error,则用户有权在不打开或选择文件的情况下取消,您根本不会继续开始操作;-)

于 2013-04-11T09:04:37.113 回答
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");
        }
    }
于 2013-04-11T09:04:47.910 回答
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");
    }
}
于 2013-04-11T09:04:51.410 回答
2
 if (dllDialog.ShowDialog() == DialogResult.OK) {
            dllDialog.ShowDialog(); // This shouldn't be here

您显示对话框两次。

于 2013-04-11T09:04:52.540 回答