当我尝试运行此代码时,它打开一个没有结果的打开对话框,然后打开另一个并执行我想要的操作。帮助
Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
Dim DidWork As Integer = openFD.ShowDialog()
openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
openFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = openFD.FileName
MsgBox(strFileName)
End If
End Sub
答案 0 :(得分:1)
您的示例中不需要DidWork变量(正如所指出的,您正在调用ShowDialog两次)。
我赞成在DialogResult中检查Ok而不是Cancel:
openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
If openFD.ShowDialog() = DialogResult.Ok Then
strFileName = openFD.FileName
MsgBox(strFileName)
Else
MsgBox("Dialog Canceled")
End If