?
Solved

Issue in FormClosing Event, VB.Net

Posted on 2014-01-09
6
Medium Priority
?
1,946 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 13

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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 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