Solved

Issue in FormClosing Event, VB.Net

Posted on 2014-01-09
6
1,690 Views
Last Modified: 2014-01-10
I have two very simple methods in my VB.Net, VS2012 project:

A button click event:
    Private Sub bExit_Click(sender As Object, e As EventArgs) Handles bExit.Click
        DialogResult = Windows.Forms.DialogResult.OK
    End Sub

A form closing event:
    Private Sub frmEditMKL_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If e.CloseReason = 3 Then
            MessageBox.Show(Me, "Please use the 'Exit' button", "Edit MKL", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        End If
    End Sub

If I open the form, and click the Exit Button, e.CloseReason = 0 and the form closes without incident.

If I open the form, and hit the X, e.CloseReason = 3 and the user is told to click the Exit button; the form does not close.

All good so far.

But, if, after I have hit the X, gotten the message, and then click on the Exit button e.CloseReason is still 3 and the form won't close.


Any ideas how I can resolve this or what is going on?
0
Comment
Question by:g_johnson
6 Comments
 
LVL 40
ID: 39769751
You should use the constants provided by the Framework instead of using numeric values. This makes the code more readable, and maybe you woud have found the answer, or at least part of it.

If e.CloseReason = CloseReason.UserClosing Then

And a CloseReason of 0 is a CloseReason.None, meaning that the reason for the closing cannot be determined, a very special situation.

What you do in the button click event cannot trigger the close of the form, there is probably some code in the click that we do not see and cause the strange behavior.

Might be some code that closes the form in a non standard way. If you click the button first, the CloseReason is not determined and the variable that holds it keeps its value of 0.

If you hit X a first time, CloseReason is set to 3. It is not reset by what happens in your click however, so it still has the value of 3 is you hit the button after hitting X.

We need to know what happens in your button.
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 39770095
I did a quick test of your code.  Although I don't know what you have in the exit button clicked event. I just did a me.close() in it.
I get e.closereason = 3 no matter if I click the 'X' or the button
Here are the FormClosingEventArgs for e.CloseReason:

ApplicationExitCall      The Exit method of the Application class was invoked.
FormOwnerClosing      The owner form is closing.
MdiFormClosing              The parent form of this multiple document interface (MDI) form is closing.
None                              The cause of the closure was not defined or could not be determined.
TaskManagerClosing      The Microsoft Windows Task Manager is closing the application.
UserClosing                      The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
WindowsShutDown      The operating system is closing all applications before shutting down.
0
 
LVL 7

Accepted Solution

by:
Ironhoofs earned 500 total points
ID: 39771049
Clicking a button that has its DialogResult set, will close the form.

You may be better of just checking if the button was clicked:

    Private Sub frmEditMKL_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If DialogResult <> Windows.Forms.DialogResult.OK Then
            MessageBox.Show(Me, "Please use the 'Exit' button", "Edit MKL", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        End If
    End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:g_johnson
ID: 39771051
What you see in the click event of the exit button as posted above is the entire code:

    Private Sub bExit_Click(sender As Object, e As EventArgs) Handles bExit.Click
        DialogResult = Windows.Forms.DialogResult.OK
    End Sub

That does indeed close the form, and it is a standard method of doing so.  In fact, I did a little quick research, and the code is not even necessary.  I just set the DialogResult property on the Exit button to OK.  That closes the form an returns the OK to the calling form.  However, it didn't solve my problem, so I made other changes (see below).

I did, as you suggest, change the FormClosing method to this:

        If e.CloseReason = CloseReason.UserClosing Then
            MessageBox.Show(Me, "Please use the 'Exit' button", "Edit MKL", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        End If

I solved it by creating a variable, _ExitButtonClicked and then doing this:

    Private Sub bExit_Click(sender As Object, e As EventArgs) Handles bExit.Click
        Me._ExitButtonClicked = True
        DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub frmEditMKL_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If e.CloseReason = CloseReason.UserClosing And Not Me._ExitButtonClicked Then
            MessageBox.Show(Me, "Please use the 'Exit' button", "Edit MKL", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        End If
    End Sub
0
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 39771061
We crossed in the mail!  Your solution is even better than mine, but both work.  Thanks for the input.
0
 
LVL 7

Expert Comment

by:Ironhoofs
ID: 39771139
Good luck and happy coding!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now