簡體   English   中英

如何關閉第一個表單並打開新表單

[英]How to close the first form and open the new one

我想在我設法打開的應用程序中打開第二個表單,但第一個表單仍在后台打開。 我怎樣才能關閉它?

private void BtnLogin_Click(object sender, EventArgs e)
{
    var newForm = new Form2();
    if (string.Equals(txtbName.Text, "Georgi") && string.Equals(txtbPassword.Text, "123"))
    {
        newForm.Show();                
    }
    else
    {
        label2.Text = "Wrong password or username.Try again";
    }
}

嘗試這個:

this.Hide();

或者,您可以使用要隱藏的表單的名稱,如下所示:

YourForm.Hide();

取決於您是只想隱藏表單還是關閉它。

對於結束,你會想要去:

private void BtnLogin_Click(object sender, EventArgs e)
{
    var newForm = new Form2();
    if (string.Equals(txtbName.Text, "Georgi") && string.Equals(txtbPassword.Text, "123"))
    {
        newForm.Show();   
        this.close(); 
    }
    else
    {
        label2.Text = "Wrong password or username.Try again";
    }
}

如果您只想隱藏它,請使用:

private void BtnLogin_Click(object sender, EventArgs e)
{
    var newForm = new Form2();
    if (string.Equals(txtbName.Text, "Georgi") && string.Equals(txtbPassword.Text, "123"))
    {
        newForm.Show();   
        this.Hide(); 
    }
    else
    {
        label2.Text = "Wrong password or username.Try again";
    }
}

當第二個表單打開時,您要關閉還是隱藏第一個表單?

在此解決方案中,您隱藏第一個表單並在第二個表單關閉時關閉它。

private void BtnLogin_Click(object sender, EventArgs e)
    {
        var newForm = new Form2();
        if (string.Equals(txtbName.Text, "Georgi") && string.Equals(txtbPassword.Text, "123"))
        {
            Form2 form = new Form2();
            this.Hide();
            form.FormClosed += (s, args) => Close();
            form.Show();            
        }
        else
        {
            label2.Text = "Wrong password or username.Try again";
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM