简体   繁体   中英

Hiding a child form close it instead and return me to the owner c#

I have a login form and after I validate the user I call the main form with the following code:

frmMain frm = new frmMain();
Hide()
frm.ShowDialog();
Show();

The problem is when I try to hide the main form with Hide() or Visible = false, to minimize the application to the tray bar, the main form gets closed and the application returns me to the login form like if I press the close button.

How does Hide() and Show() work? Does it create another instance?

When a modal form is hidden, it would return to the calling code. That's how it is. What i would do is change the code to either close the login form after showing the main form:

frmMain frm = new frmMain();
frm.Show();
Close()

If you want to show again the login form when the main form is closed, you could use the FormClosing event, like this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    frmLogin frmlogin= new frmLogin();
    frmlogin.Show();
}

Or call the main form in the beginning, and from it call the login form as a dialog.

Make two window form, 1:- Login (default open), 2:- Main (Open on login success)

1:- First login window will open 2:- after successfully login close/hide the login form and open/show the Main form which will also be window. 3:- handle the task bar display property in the same way.

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