Solved

Issue in FormClosing Event, VB.Net

Posted on 2014-01-09
6
1,834 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 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
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

705 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