我有一个Form1,当我们关闭Form1和Form2显示时,我添加了2个标签,"你确定吗"以及";你真的想退出吗"并且存在3个被称为"按钮"的按钮;是"否";以及";我不在乎;。我知道如何使用消息框处理关闭表单事件,它是:
MessageBox("Do you really want to exit?", "Are you sure?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
e.Cancel = True
Else
e.Cancel = False
但我不知道如何停止从Form2关闭Form1。提前感谢!
以下是我如何解决这个问题,在Form1:中使用此代码
Public Class Form1
'handle the Closing event of Form1 to be able to cancel the closing
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
'if Form2 returns a dialog result of Cancel, cancel the closing operation, otherwise let it continue
If Form2.ShowDialog = DialogResult.Cancel Then
e.Cancel = True
End If
End Sub
End Class
这个代码在Form2:中
Public Class Form2
'if the Ok button is clicked, return a dialog result of OK
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
DialogResult = DialogResult.OK
End Sub
'if the cancel button is clicked, return a dialog result of Cancel
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
DialogResult = DialogResult.Cancel
End Sub
End Class
根据用户在Form2中单击的按钮,Form1将关闭或保持打开状态。
如果你有任何问题,请毫不犹豫地对我的回答发表评论,我很乐意进一步指导你。