我有一个代码片段,我想在应用程序关闭时运行它。所以,我使用了FormCLosing事件。但现在我想放一条退出的确认消息。例如,如果用户单击Exit(X)按钮,将会有一个提示,如果他单击NO,则应用程序将不会关闭并恢复到以前的状态。
现在我发现使用FormClosing事件很难做到这一点。因为无论用户单击哪个按钮,它都会被执行。有什么补救措施吗?
我是说,我需要一个像ExitButtonPressed()这样的人..
发布于 2010-02-13 15:51:09
你可以尝试像这样的东西
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If (MessageBox.Show("Close?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No) Then
e.Cancel = True
End If
End Sub看一看
FormClosingEventArgs Class
和
CancelEventArgs.Cancel Property
通过将Cancel属性设置为
,可以取消该事件。
发布于 2017-11-20 02:48:12
表单的Button2和closebutton都会关闭表单,询问相同的问题
Dim button2Yes As Boolean = False私有子Button2_Click(发送者作为对象,e作为EventArgs)句柄按钮2.单击
If MessageBox.Show(" Sure to close? ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
button2Yes = True
Me.Close()
Else
button2Yes = False
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.FormClosing
If Not button2Yes Then
If Not MessageBox.Show(" Sure to close? ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = True
End If
End If
End Subhttps://stackoverflow.com/questions/2256869
复制相似问题