我在vb.net中为桌面应用程序创建了一个函数,在关闭之前完全保存了文档,我用System.Threading.Timer实现了它。
我请求你帮我说说你是怎么经历的。
所以,我做了一个计时器,在关闭前保存文件。
但是,在没有计时器的情况下进行测试时,只要调用一个函数或Handler,文档就不会在关闭之前保存,因为会出现一个对话框,要求保存或不保存或取消。
真的需要计时器吗?因为我们知道计时器可以延迟系统甚至一毫秒。
我用100毫秒设置了计时器。
但是,我不想使用计时器,我想在关闭之前保存文档,只用于函数的一个调用。
它真的使用计时器是解决办法吗?或者只需要一个呼叫设置就可以完成?
如果它可以在一个函数调用中完成,那么我想我遗漏了一个代码。
感谢您的意见和经验。
发布于 2013-06-16 21:20:18
您可以处理FormClosing事件。在处理程序中,可以在窗体关闭之前执行操作,甚至可以取消关闭事件。下面是一个例子:
Dim Saved As Boolean = False
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not Saved Then
Dim Result As DialogResult = MessageBox.Show("Do you want to save your text?", "Save Text?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Select Case Result
'Since pressing the No button will result in closing the form without
'saving the text we don't need to handle it here
Case Windows.Forms.DialogResult.Yes
SaveText_Click()
Case Windows.Forms.DialogResult.Cancel
e.Cancel = True
End Select
End If
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Saved = False
End Sub
'SaveText is a button for the user to manually save the text on demand.
'Since we don't need the sender or the eventargs, we can handle the click event without them.
'this way we can call this like any sub without having to worry about providing the right parameters.
Private Sub SaveText_Click() Handles SaveText.Click
'Add your procedure to save the text here
Saved = True
End Sub
如果不想在不保存的情况下关闭选项,只需省略messagebox和select块,只需调用SaveText(),并包含保存数据的代码。
https://stackoverflow.com/questions/17132606
复制相似问题