VB.net SaveFileDialog需要取消两次

时间:2015-11-12 18:43:57

标签: vb.net savefiledialog

我想在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

1 个答案:

答案 0 :(得分:2)

您正在显示对话框两次,因此请尝试仅显示一次:

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