Exiting a module

hotblack

New member
Joined
Nov 26, 2004
Messages
1
Programming Experience
Beginner
Hi I have a quick VB.NET question...
a) How do I close a module in a multiform program.
I tried Me.Close() but it says it can't use it in modules.
b) How do I control little red X button on forms? What if instead of closing the program I want to hide the form when the user presses on red X in the top right corner?

Thanks in advance.
 

Joshdbr

Well-known member
Joined
Apr 7, 2011
Messages
211
Location
Montana, USA
Programming Experience
5-10
For the exit button, add something like the following code to the forms Closing event.

VB.NET:
 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = True
    Me.Hide()
End Sub
Note that the FormClosing event is not triggered if you use a Application.Exit() or End.

For the module question, I'm not sure, but I believe that just an Exit Sub() would do it if there wasn't anything else running.

-Josh
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,322
Location
Sydney, Australia
Programming Experience
10+
Note that the FormClosing event is not triggered if you use a Application.Exit() or End.
You should never, ever use End anyway, so that should never be an issue. As for Application.Exit, it's the Closing and Closed events that aren't raised. FormClosing and FormClosed are raised when you call Application.Exit, which is the primary reason they were added in .NET 2.0.

Joshdbr's code therefore has an issue: it will never let you close the form. You would have to make use of the e.CloseReason property to determine whether to hide or close, or else use your own flag. (EDIT: or use the Closing event instead, in which case you can use Application.Exit to exit the app and close your form)

As for closing a module, you don't. You never open a module so you never close one. Modules are not like a form, where you display it on-screen and you have to explicitly remove it form display. A module is simple a collection of members. You call one of those members, execution enters the member, it completes and execution exits the member. That's it. You don't do anything other than call the member.
 

Joshdbr

Well-known member
Joined
Apr 7, 2011
Messages
211
Location
Montana, USA
Programming Experience
5-10
You are right about not using End, but it was included in Microsoft's spill on the whole thing so I included it. I didn't realize there was a Closing and Closed event, they aren't in the Forms events.

Joshdbr's code therefore has an issue: it will never let you close the form.
Not closing and hiding instead is what the original post asked for:

How do I control little red X button on forms? What if instead of closing the program I want to hide the form when the user presses on red X in the top right corner?
I am a little confused about the module closing, I have had problems in the past with a module not closing even after all of the events were finished. What would cause that?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,322
Location
Sydney, Australia
Programming Experience
10+
You are right about not using End, but it was included in Microsoft's spill on the whole thing so I included it. I didn't realize there was a Closing and Closed event, they aren't in the Forms events.
Did you check the documentation? The fact that the IDE doesn't expose them doesn't mean that they don't exist. They were essentially replaced by FormClosing and FormClosed in .NET 2.0 so they are not listed in the IDE from VS 2005. The old events still exist but the new ones should be used in preference in almost all case.
Not closing and hiding instead is what the original post asked for
That's all well and good but you're going to want to close every form at some point, even if it's just when you're shutting down the OS. Your code would prevent Windows exiting without forcibly ending the app's process.
I am a little confused about the module closing, I have had problems in the past with a module not closing even after all of the events were finished. What would cause that?
There is no such thing as closing a module. You can't close something that you can't open.
 

Joshdbr

Well-known member
Joined
Apr 7, 2011
Messages
211
Location
Montana, USA
Programming Experience
5-10
OK, reworded a little: the application doesn't exit after the module is done running.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,322
Location
Sydney, Australia
Programming Experience
10+
OK, reworded a little: the application doesn't exit after the module is done running.
That has nothing to do with the module. Again, modules don't run. A module is simply a collection of members. You can one or more of those members and they execute. Once a called method has executed, that's it. There's nothing of the module left behind to prevent your application exiting.

It's impossible to know what might have prevented such an application exiting from so little information, but one possibility is a foreground thread. When new threads are started, they are foreground by default but they can be made background. The one and only difference between a foreground and background thread is that a process cannot exit while a foreground thread is running, while exiting a process will automatically terminate any background threads still running. If you were to start a new foreground thread and then try to exit the application, the process would not exit until that thread completed.
 

Joshdbr

Well-known member
Joined
Apr 7, 2011
Messages
211
Location
Montana, USA
Programming Experience
5-10
Thanks for replying, it was a silly question :)
 
Top Bottom