Kapalic

How can I prevent an user to close the form clicking on the (X) button

Thnx!
Kapalic




Re: Windows Forms General Form closing preventing

Frank Carr

My first question is why don't you want the user to close the form From a UI perspective the behavior should be the same as clicking a Cancel button and it's best to code your application where both events use the same code. But, you may have a form that you don't want the user to cancel or at least not cancel right away.

Setting the ControlBox property to False is one way to do this. Since you don't want to user to have this ability, just don't make it visible. You can also toggle this property setting at runtime (unlike in VB6). If you want to disable the button instead, this thread explains how to do it in VB.Net






Re: Windows Forms General Form closing preventing

Gavin Jin - MSFT

Hi,kapalic

Here is the code to prevent closing form.

Private Sub FormClosing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

e.Cancel = True

End Sub

Best Regards!






Re: Windows Forms General Form closing preventing

Kapalic

I want user only close the form with my given button. How can I detect that cross button was pressed Please show me an example.

Thnx!
Kapalic






Re: Windows Forms General Form closing preventing

Frank Carr

Your form closing code should never be in any event other than the Form_Closing or Form_Closed event. Otherwise your closing code will never be executed if the application is closed indirectly, such as through Task Manager or Windows shutting down. The first thing to do is to put or call your closing code only from one of these events. In both you can use e.CloseReason to determine where the close message came from and in Form_Closing you can use e.Cancel = True to prevent the form from closing.