How to Close a Login Form and Show the Main Form Without My Application Closing

How can I close a login form and show the main form without my application closing?

The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). Check out the documentation for that method here on MSDN, which explains this in greater detail.

The best solution is to move the code out of your login form into the "Program.cs" file. When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). When the login dialog closes, you'll check its DialogResult property to see if the login was successful. If it was, you can start the main form using Application.Run (thus creating the main message loop); otherwise, you can exit the application without showing any form at all. Something like this:

static void Main()
{
LoginForm fLogin = new LoginForm();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}

How to hide or close startup loginform after main form is open?

use this

  static void Main()


{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLog ());

}

inside frmlog

use

this.hide();
new frmMain().ShowDialog();

mainform trying to show on closing the login form

Instead of hiding Main Form before you show Login you should use initialize Application to start with Main Form hidden. To do that just add Application.ShowMainForm := false before you create main form.

Application.Run that executes after you close your login form will show or not show Main Form based on value of Application.ShowMainForm variable that is by default true. So that will introduce flicker because main form will be shown before application terminates.

begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.ShowMainForm := false;
Application.CreateForm(TForm1, Form1);
Login;
Application.Run;
end.

how to close only the main form in vb.net 2010 without terminating the application

I hope this makes sense....

It sounds like you have a VB.Net project and your Login form is your 'startup form'. When you close that form, your application thinks it is over; but you really want to take action after the Login form is closed.

If you bring up the Properties window for the project, on the Applications Tab you can set the 'Shutdown mode'. The default is when the 'Startup Form Closes'. Change it to 'When the last form closes'.

You can also add Application level events here.
http://msdn.microsoft.com/en-us/library/f2bys999(v=vs.80).aspx

If you stick with the way you are going; your Login form is going to have to create another form before it closes or your app will close. You can do that; but it's probably cleaner to move the login logic into the Application Startup Event (see link for more details).

In the startup event you can show the Login screen, get the result, decide if you want to show the main form for your application, etc, etc...



Related Topics



Leave a reply



Submit