3

i have developed a windows form application using c#.

it has 2 forms like login form and main form. When i enter the correct login credentials it should close(not hide) the login form and display the main form.

i used the following code

MainForm main=new MainForm();
this.hide();//close login form
main.show();//display main form

but when I close the main form using the cross mark(right upper corner) in the mdi container, the main form closes but the application is still running in the task manager.

If I use the following code instead of the previous code, application will close before main form display.

this.close()//close login form
main.show();//display main form

do i have to hide mdi container from the main form or is there any way to achieve this? please help.

sim1
  • 722
  • 7
  • 12
sanzy
  • 805
  • 9
  • 18
  • 28

5 Answers5

5

Try like this:

this.Hide();
Main.ShowDialog();
this.Close();

First, you hide the login form. Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed. Last, once the dialog is closed, you close the login form, ending the application.

Sathish
  • 4,419
  • 4
  • 30
  • 59
  • I agree that this somewhat works the way the OP wanted but this is what the OP didn't want to do. He or she wanted to close(Not Hide) the login form. See my answer below for a method where the login form will be closed and launches the main form when a successful login occurs. – waltmagic Jan 09 '15 at 20:41
1

The application is still running because you still have one form that is still alive but hidden.

You can subscribe the Close event in MainForm and manually exit the application through Application.Exit().

Another option is to make sure there is only one window alive: open MainForm in the handler of the LoginForm.Close event as described here: Windows Forms: Change application mainwindow at runtime

MyForm1 f = new MyForm1();
f.Close += OnOpenOverviewWin();
Application.ShutdownMode = ShutdownMode.OnLastWindowClose;
Application.Run(f);

void OnOpenOverviewWin()
{
  if (loginok)
  {
    MyOverViewForm f = new MyOverViewForm ();
    f.Show();
  }
}
Community
  • 1
  • 1
sim1
  • 722
  • 7
  • 12
0

You need to show main form before closing login form. try this:

main.show();//display main form
this.close()//close login form
Itz.Irshad
  • 1,014
  • 5
  • 23
  • 44
  • Have you tried this ishad,. when i am trying this code my hole application has closed. – Sagotharan Mar 31 '14 at 05:08
  • Mr. Irshad please correct this answer, your is not correct, because when you show another form from root form, and closed it root form, it always closed hole application. – Durgesh Pandey Mar 05 '16 at 16:27
0

What I always do is :

MainForm main=new MainForm();
Visible = false;
main.Show();

and when in my main form I set the form_closed event handler to Application.Exit(); like this:

private void main_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.Exit();
}

so when the user click close the main all application stops

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
jafaritaqi
  • 578
  • 7
  • 14
0

I think the accepted answer is incorrect to some extent. This is the actual answer the OP was looking for. Sorry it took a year to get answered. Inside of your Main() method in your project's Program.cs file, copy and paste the following at the bottom:

        Logon logonForm = new Logon();
        if(logonForm.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Portal());             
        }

Inside of your logonForm when the user is authenticated or has a successful login event set the form's DialogResult = DialogResult.OK Here is an example of this:

    private void logonButton_Click(object sender, EventArgs e)
    {
        string username = usernameTextBox.Text;
        string password = passwordTextBox.Text;

        if(logon(username, password))
        {
            MessageBox.Show("Logged On Successfully!", "Success",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        else
        {
            MessageBox.Show(getFailureReason(), "Failure",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
waltmagic
  • 631
  • 2
  • 9
  • 22