Closing Form and exiting application

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi, I have a dilemma, I am writing a windows form app in vb.net 2010 visual basic, but here my dilemma

I have a main switch board form that I want to close when the user I click's a button to open other forms.

But on my main form I have an application exit button which handles the closing the form and application exit.

But if the user clicks on the form close button [X] the main switchboard form closes, but the application stays active/open.

I have tried a number of solutions but each one opens up a set of different problems.

ANy ideas appreciated
 
Hi,

The only Form that will actually Exit your Application when closing using the ControlBox is the StartUp Form specified in your Project Properties so if you are opening and closing other forms then these will not Exit the Application when closing those forms.

Hope that helps.

Cheers,

Ian
 
It's not really clear exactly what you want and exactly what you are currently seeing so maybe if you were to provide a FULL and CLEAR description of what you would like to happen.

That said, application shutdown behaviour is fairly simple. In the project properties you have two options: shutdown when startup form closes or shutdown when all forms close. If you select the first option, which is the default, the application will exit as soon as you close the startup form by any means. If you choose the second option then the application will exit as soon as there are no forms open. It's important to note that, if you only have one form open and that form calls its own Close method, there are no longer any forms open and the app will exit, even if that form shows another form immediately after calling Close.
 
Closing form but not app

Its difficult to explain, but I 'll have another go...

I have the startup log on form which stays open all the time and has all the global variables. When the user logs in I open the switch board form, on the switch board form is the Application exit button, with 6 other button to open other forms.

That all works fine, except if the user click the control box close n the switch board form, it closes but app still running.

If I put in code into the forms close events it get executed when the user doesn't want to close the app but want to open another form ie.

Form4.Show
Me.Close

I am currently using me.hide but would prefer to close the form thus saving memory space and dispose of all datasets etc...

Any advice appreciated...
 
Why do you leave the login form open all the time? Does the user actually need it open all the time or is that just a hack to make the whole thing work? It sounds quite wrong to me.
 
Hi,

I have a module in form1 that has al the global variables, I suppose I could relocate them to another form or a seperate module?

Should all forms be closed?
 
The login form is for logging in and that's it. If you need global variables then you define a module for global variables. It's an independent type like any other. It doesn't go in anything else. Personally, on the rare occasions that I actually need a global variable, I declare it in the MyApplication class, so it's accessed in code like My.Aplication.SomeGlobalVariable.

Your main form should be your startup form and, if you need a login form displayed before that, it should be displayed from the Startup event handler of the application.

WinForms Login
 
Back
Top