?
Solved

Issue in FormClosing Event, VB.Net

Posted on 2014-01-09
6
Medium Priority
?
1,875 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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