Jack Pollock
Jack Pollock

Reputation: 334

VB.NET Disable/Hide x button but WITHOUT disabling closing altogether

I would like to remove/disable/hide the x button from my form but WITHOUT disabling closing altogether. So I want it so the program can't be closed by using the x button but ONLY by a certain key command (Through a menu item or similar). The only way I know of is to disable closing altogether which then means that the x button doesn't work but I cannot close it using the key command.

Are there any ways of disabling JUST the x button?

Thanks.

Upvotes: 0

Views: 2840

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

In the properties for the form, set the ControlBox property to False. Then, in the code, when you want to close the form, just call the form's Close method.

However, doing that will not stop the user from closing the window via standard OS methods (e.g. via the button on the task bar, via ALT+F4). In order to stop that, you would need to cancel the closing of the form in its FormClosing event. For instance:

Public Class Form1
    Private _closeAllowed As Boolean = False

    Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
        _closeAllowed = True
        Close()
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Not _closeAllowed Then
            e.Cancel = True
        End If
    End Sub
End Class

However, even that won't stop the application from being terminated. For more thorough solutions, you may want to do some searches on best-practices for developing kiosk applications for Windows.

Upvotes: 1

Related Questions