用户取消时保存对话框
Posted
技术标签:
【中文标题】用户取消时保存对话框【英文标题】:Save Dialog when User Cancel 【发布时间】:2014-03-16 02:21:14 【问题描述】:我需要在我的应用程序的不同位置保存一个文件,所以我为此创建了一个子文件;一切正常,除非用户在保存对话框出现时单击取消;如果用户单击“取消”,表单将关闭;我尝试了下面代码中显示的两个选项,但都不起作用;任何建议将不胜感激:
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
结束类
【问题讨论】:
再次打开表单?Form1.Show
.
【参考方案1】:
将你的 SaveFileDialog 子例程变成一个函数,如果除了 OK 以外的任何事情都完成了,则返回 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
【讨论】:
你是男人马克;下次我在加利福尼亚的时候应该去拜访你:-) 试图在该州抓住我可能会很有趣 :),下周佛罗里达州,然后是威斯康星州......很高兴我能得到帮助。【参考方案2】:不太清楚你需要什么...试试这个:
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
【讨论】:
你说得对 ElektroStudios,我的问题不清楚,所以我修改了代码以准确显示问题。如果使用了关闭表单,它会显示保存对话框,如果用户在对话框上单击 CANCEL,表单将关闭,如果用户单击 CANCEL,我不希望它关闭。以上是关于用户取消时保存对话框的主要内容,如果未能解决你的问题,请参考以下文章