简体   繁体   中英

closing password form c#

How can I close the password form when my new form opens?

public partial class Password : Form
{
    private string password;
    public Password()
    {
        InitializeComponent();
    }

    private void pass_TextChanged(object sender, EventArgs e)
    {
        password = "1234";
    }

    private void okButton_Click(object sender, EventArgs e)
    {
        if (passtextBox.Text == password)
        {
            list form = new list();
            form.Show();

        }
       else
        {
            MessageBox.Show("Incorrect Password. Try Again!!");
        }
    }
}

When I use this.close(); my new form and password form both close. What should I do?

I assume your PasswordForm is the main form for you which you passsed inside Application.Run method.

So when the main form closes application will exit.

I'd suggest you to just hide the form instead of closing it.

list form = new list();
form.Show();
this.Hide();

You can use MDI form as the parent form. When a new form is created and that new form(which is a child form of the MDI form) is opened over the MDI or any parent form, then you can search for all the opened child form. If any child form is found open, then close that child form. In this way you can manage form opening and closing.

Thanks.

You want to show new form and close first form if password is correct, don't you? Try this:

 Form secondform = new form();
 Secondform.show();
 Form1 firstform = new form1();
 Firstform.hide();

i fixed it myself

public partial class Password : Form { private string password; public Password() { InitializeComponent(); }

    private void pass_TextChanged(object sender, EventArgs e)
    {
        password = "1234";
    }

    private void okButton_Click(object sender, EventArgs e)
    {
        if (passtextBox.Text == password)
        {
         // list form = new list();
         //form.Show();
             //list secondform = new list();
              //secondform.Show();
              //Password firstform = new Password();
            // firstform.Hide();
           this.Hide();
            list sistema = new list();
            sistema.ShowDialog();
            this.Close();


        }
       else
        {
            MessageBox.Show("Incorrect Password. Try Again!!");
        }
    }




}

}

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