VB.net SaveFileDialog 需要取消两次

VB.net SaveFileDialog need to Cancel twice

我想在您点击 SaveFileDialog 中的“取消”时更改文本标签。我一切正常,只有一个问题,当我点击取消时,SaveFileDialog 将再次弹出,我需要再次点击取消。

当我添加这段代码时,我需要按取消两次,没有这段代码它工作正常。

    If SaveFileDialog1.ShowDialog() = DialogResult.Cancel Then
       Label1.Text = "Not Saved"
    End If

我保存的完整代码:

    Label1.Text = "Saving..."
    TextBox1.Visible = False
    SaveFileDialog1.InitialDirectory = "C:/"
    SaveFileDialog1.Title = "Save Your Results"
    SaveFileDialog1.FileName = Label2.Text
    SaveFileDialog1.Filter = ("text files (*.txt) | *.txt")

    SaveFileDialog1.ShowDialog()

    Dim w As New IO.StreamWriter(SaveFileDialog1.FileName)
    Dim i As Integer
    For i = 0 To ListBox1.Items.Count - 1
        w.Write(ListBox1.Items(i).ToString)
    Next
    w.Close()

    Label1.Text = "Saved"
    If SaveFileDialog1.ShowDialog() = DialogResult.Cancel Then
        Label1.Text = "Not Saved"
    End If

您显示了两次对话框,因此请尝试只显示一次:

If SaveFileDialog1.ShowDialog() = DialogResult.Ok Then
  Dim w As New IO.StreamWriter(SaveFileDialog1.FileName)
  Dim i As Integer
  For i = 0 To ListBox1.Items.Count - 1
     w.Write(ListBox1.Items(i).ToString)
  Next
  w.Close()
  Label1.Text = "Saved"
Else
  Label1.Text = "Not Saved"
End If