我试图阻止SaveFileDialog从elseif提示DialogResult.No(并取消)

时间:2013-03-25 19:10:45

标签: prompt savefiledialog

基本上我是这方面的初学者,我正在尝试通过创建自己的Note Pad应用程序来练习,同样复制Microsoft的记事本。我在网上搜索了几个小时,但没有占上风。

所以问题是,我从ToolStripMenu中的项目'New'开始,按下时,如果用户按下是,它会提示用户“是否要保存对此文档的更改?” ,SaveFileDialog显示,用户可以将其保存到他/她喜欢的任何地方。如果用户按否,则清除RichTextDialogBox中的信息,如果用户按下取消,则返回,而不按计划进行任何更改。但是,当我单击“否”和/或“取消”按钮时,它仍然会提示用户使用SaveFileDialog框。是否有人可以帮助我稍微更改我的代码,以便在提示用户使用SaveFileDialog时停止按钮“否”和“取消”?

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
    Dim intResult As Integer = Nothing
    Dim dlg As SaveFileDialog = New SaveFileDialog
    dlg.Filter = "Text File (*.txt)|*.txt"
    intResult = MessageBox.Show("Do you want to save the changes made to this document?", "Warning", MessageBoxButtons.YesNoCancel)
    If dlg.ShowDialog = Windows.Forms.DialogResult.Yes Then
        RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
    ElseIf intResult = Windows.Forms.DialogResult.No Then
        RichTextBox1.Clear()
    End If
    If intResult = Windows.Forms.DialogResult.Cancel Then
        Return
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我认为这是因为您将对话框作为If条件测试的一部分进行调用

尝试以下

Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

    Dim intResult As Integer = Nothing
    Dim dlg As SaveFileDialog = New SaveFileDialog

    dlg.Filter = "Text File (*.txt)|*.txt"
    intResult = MessageBox.Show("Do you want to save the changes made to this document?", "Warning", MessageBoxButtons.YesNoCancel)

    If intResult = Windows.Forms.DialogResult.Yes Then
        dlg.ShowDialog
        RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
    ElseIf intResult = Windows.Forms.DialogResult.No Then
        RichTextBox1.Clear()
    Else
        Return
    End If

End Sub