简体   繁体   中英

How to close a form from a user control parent?

So, I put some user control into my C# Form, because I want some tabs (such as Login, info, and version) to show different "screens" when they get opened. For example, when I click Login, it shows you the user control where you have to put your key to login, on version tab it shows you some labels with version of the tool etc.

I want that when the key is correct, in the login_page UserControl, the form where the user control is (Form1) gets closed, and the Form2 opens.

I tried using ParentHide(); , but it doesn't close the form at all, it just make it all blank, like empty. I also tried this.Close(); and Application.Exit(); , but in user control they don't seem to be working.

MessageBox.Show("Key is working.", "Login System");
                Parent.Hide();
                form2`` f2 = new form2();
                f2.ShowDialog();

this.Close() , for example, gives error CS1061 and says that login_page (the user control's name) doesn't contain a definition of Exit etc...

You Can Use FormClosing Event For Resolve Problem.

 private void Form1_Load(object sender, EventArgs e)
 {
     this.FormClosing += new FormClosingEventHandler(FormClosingEvent);       
 }

 private void FormClosingEvent(object sender, FormClosingEventArgs e)
 {
     form2 f2 = new form2();
     f2.ShowDialog();

 }

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