0

当我尝试运行此代码时,它会打开一个没有结果的打开对话框,然后它会打开另一个并执行我希望它执行的操作。帮助?

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
4

1 回答 1

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
于 2013-10-03T18:35:15.683 回答