简体   繁体   中英

How to open a new window form within another form in c#

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.

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.

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();
  }
}

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

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

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

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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM