Solved

Issue in FormClosing Event, VB.Net

Posted on 2014-01-09
6
1,664 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

23 Experts available now in Live!

Get 1:1 Help Now