选择.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");
}
}
答案 0 :(得分:2)
你不应该拨打dllDialog.ShowDialog()
两次,而是使用它:
if (dllDialog.ShowDialog() == DialogResult.OK)
{
tbRepTempLibrary.Text = dllDialog.FileName;
}
如果用户单击取消因为不想选择文件而不显示error
,则用户有权在不打开或选择文件的情况下取消,您只是不会继续执行操作; - )
答案 1 :(得分: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 :(得分: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");
}
}
答案 3 :(得分:2)
if (dllDialog.ShowDialog() == DialogResult.OK) {
dllDialog.ShowDialog(); // This shouldn't be here
您将对话框显示两次。