Disable Win Form Close button [X]

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi,

I thought I had already posted this but could not find it.

Is there some way to disable the Windows Form Close button [X], I have provided a separate button which closes the form and open's another and does all the connection closing and disposing.

I am using MS Sql 2008 r2 and vb.net express 2010

Thanking you...
 
Hi, Thanks for the replies. I will try them out.

I'm now also thinking tho that I might keep the [X] control and use a boolean variable to test if form closed using [X] rather than a close button then exit the application altogether. As user might need some quick way of exiting application without need to return to main menu to many clicks if you know what I mean...

But then I need to dispose of sql connection, dataset etc.

Not sure what is best option would like to know what others have done in the past?

regards to all...
 
I was answering your question literally but I'd certainly go with what Solitaire suggested if you are OK with losing the Minimize and Maximize buttons as well as the system menu on the form and Taskbar icon.

You can do all the cleanup in the FormClosed event handler, which will be executed no matter how or why the form was closed.
 
Closing an application

You can do this instead. Enter code in the Form Closing event. This will work whether you use an exit button or the X on the control box.

VB.NET:
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim ans As DialogResult
        ans = MessageBox.Show("Are you sure you want to close the application?", "Close confirmation", MessageBoxButtons.OKCancel)
        If ans = Windows.Forms.DialogResult.OK Then
            MessageBox.Show("Closing open files")  'take care of other business before ending
            e.Cancel = False
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
 
You can do this instead. Enter code in the Form Closing event. This will work whether you use an exit button or the X on the control box.

VB.NET:
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim ans As DialogResult
        ans = MessageBox.Show("Are you sure you want to close the application?", "Close confirmation", MessageBoxButtons.OKCancel)
        If ans = Windows.Forms.DialogResult.OK Then
            MessageBox.Show("Closing open files")  'take care of other business before ending
            e.Cancel = False
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
Be sure to add a check in your code for the e.CloseReason
If the system is shutting down, for example, you don't want that message to show and delay/prevent the system from said shut down.
 
Hi Thanks for the code solitaire, appreciate it.

My situation is this, when user click on my close button (not the [X]) I want to close all connection's dispose of dataset etc. do open another form and close the current form.

If the users clicks on the form [X] control box I want to do some of above, but not open another form, and the exit the application normally.

The dilemma is when I try to do this I end up in an infinte loop of closing calling closing...does that make sense?

So I think I need a boolean bExitApp and if true execute code in Form Closing. If false close current form open another form and do not exit app...

Any suggestions how it can be done an easier way?
 
You could try using a form-level Boolean variable to test if all business is finished.

VB.NET:
Public Class Form1

    Private finished As Boolean

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim ans As DialogResult
        'If all business is taken care of Then 
        '   finished = True 
        'Else 
        '   finished = False
        'End If
        ans = MessageBox.Show("Are you sure you want to close the application?", "Close confirmation", MessageBoxButtons.OKCancel)
        If ans = Windows.Forms.DialogResult.OK Then
            If finished = False Then
                MessageBox.Show("Closing open files")  'take care of other business before ending
                e.Cancel = False
            Else
                e.Cancel = False
            End If
        Else
            e.Cancel = True
        End If
    End Sub
End Class
 
Back
Top