用户取消时保存对话框

时间:2014-03-16 02:21:15

标签: vb.net save

我需要在我的应用程序中保存来自不同位置的文件,所以我为此创建了一个子文件;一切正常,除非用户在保存对话框出现时单击取消;如果用户点击“取消”,表格将关闭;我尝试了下面代码中显示的两个选项但两个都没有用;任何建议将不胜感激:

   Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SaveFileDialog()
End Sub

Sub SaveFileDialog()
    SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        Dim MekdamSaveFile = SaveFileDialog1.FileName
        System.IO.File.WriteAllText(MekdamSaveFile, "")
        My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
    End If
End Sub


Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Dim result = MessageBox.Show("The File:  has been changed, do you want to save it? ", _
    "Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
    If result = DialogResult.Cancel Then
        e.Cancel = True
    ElseIf result = DialogResult.No Then
        'PROCEED...
    ElseIf result = DialogResult.Yes Then
        SaveFileDialog()
    End If
End Sub

结束班

2 个答案:

答案 0 :(得分:2)

将SaveFileDialog子例程转换为函数,如果完成了其他任何操作,则返回False,然后在FormClosing EventHandler中测试它并停止关闭。

修改后的SaveFileDialog

Function SaveFileDialog() As Boolean
    SaveFileDialog1.Filter = "TXT Files (*.txt)|*.txt"
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        'If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        Dim MekdamSaveFile = SaveFileDialog1.FileName
        System.IO.File.WriteAllText(MekdamSaveFile, "")
        My.Computer.FileSystem.WriteAllText(MekdamSaveFile, RichTextBox2.Text, True)
        Return True 'Return True if Ok is clicked
    Else
        Return False 'return false this will give you something to conditionaly test
    End If
End Function

修改了FormClosing EventHandler

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Dim result = MessageBox.Show("The File:  has been changed, do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
    If result = DialogResult.Cancel Then
        e.Cancel = True
    ElseIf result = DialogResult.No Then
        'PROCEED...
    ElseIf result = DialogResult.Yes Then
        If Not SaveFileDialog() Then e.Cancel = True 'this will abort the close
    End If

End Sub

答案 1 :(得分:0)

不太清楚你需要什么......试试这个:

Public Class Form1

Private Sub Test() Handles MyBase.Load

    Do Until Not ShowSaveFileDialog() = DialogResult.Cancel
        ShowSaveFileDialog()
    Loop

End Sub

Private Function ShowSaveFileDialog() As DialogResult

    Using SFD As New SaveFileDialog With
        {
         .Filter = "TXT Files (*.txt)|*.txt",
         .ValidateNames = True
        }

        AddHandler SFD.FileOk, AddressOf SFD_FileOk
        Return SFD.ShowDialog()
        RemoveHandler SFD.FileOk, AddressOf SFD_FileOk

    End Using

End Function

Private Sub SFD_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs)

    IO.File.AppendAllText(sender.FileName, RichTextBox2.Text, System.Text.Encoding.Default)

End Sub

End Class