簡體   English   中英

無法在 C# 中通過 form.close() 關閉表單

[英]can not close form by form.close() in c#

我想通過一個按鈕通過另一個表單打開一個表單並關閉當前表單。 我找到了如何做到這一點,但我嘗試了自己的方式,但無法做到。 為什么此代碼不起作用

private void Button1_Click(object sender, EventArgs e)
    {

            Advise1 form = new Advise1();
            form.Show();
            this.Close();


    }

我知道如何解決我的問題,但我想知道為什么 close() 方法不是 close() 第一種形式。 當我嘗試這種方式時,單擊按鈕后會打開兩個表單。

你沒有說你的問題或錯誤是什么,但答案很可能在你的主程序的入口點 - 通常在 program.cs 中:

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form1 = new Form1();
        Application.Run(form1);
    }
}

Application.Run(form1); 基本上是說“這個 Form1 實例是我的應用程序,一直運行直到這個表單關閉。”

當你調用this.Close(); 您正在關閉 Form1 實例,這基本上是在關閉整個應用程序。

暫無
暫無

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

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