防止表格关闭

时间:2019-02-22 20:41:31

标签: vb.net winforms

我需要“禁用” X按钮,以便它仅发送一条消息,要求您关闭我创建的其他按钮。

我尝试过:

Private Sub Form2_Closing(sender As Object, ByVal e As CancelEventArgs) Handles MyBase.Closing
    e.Cancel = True
    MessageBox.Show("Cierra Usando el boton SALIR", "Atención", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Me.Close()
End Sub

但是现在我无法关闭表格

1 个答案:

答案 0 :(得分:1)

您需要一个变量:

Private okToClose As Boolean = False

在用户单击“关闭”按钮时进行设置:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  okToClose = True
  Me.Close()
End Sub

然后检查值:

Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
  If Not okToClose Then
    MessageBox.Show("Cierra Usando el boton SALIR", "Atención",
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
    e.Cancel = True
    MyBase.OnFormClosing(e)
  End If
End Sub