簡體   English   中英

按鈕創建一個按鈕,然后該按鈕顯示/隱藏表單

[英]Button creates a button then that button show/hide form

我已經知道如何在單擊按鈕時創建按鈕。 我應該在這行旁邊寫什么代碼才能顯示/隱藏 forms?


                    Button b1 = new Button();
                    b1.Location = new Point (21, 0);
                    b1.Name = "";
                    b1.Size = new Size(120, 100);
                    b1.FlatStyle = FlatStyle.Flat;
                    b1.Image = TITOMS_LOGIN.Properties.Resources.icon1_1_;
                    b1.BackColor = Color.Transparent;

                    Button b2 = new Button();
                    b2.Location = new Point(21, 99);
                    b2.Name = "";
                    b2.Size = new Size(120, 100);
                    b2.FlatStyle = FlatStyle.Flat;
                    b2.Image = TITOMS_LOGIN.Properties.Resources.icon2_1_;
                    b2.BackColor = Color.Transparent;

                    Button b3 = new Button();
                    b3.Location = new Point(21, 198);
                    b3.Name = "";
                    b3.Size = new Size(120, 100);
                    b3.FlatStyle = FlatStyle.Flat;
                    b3.Image = TITOMS_LOGIN.Properties.Resources.icon3_1_;
                    b3.BackColor = Color.Transparent;

                    Button b4 = new Button();
                    b4.Location = new Point(21, 297);
                    b4.Name = "";
                    b4.Size = new Size(120, 100);
                    b4.FlatStyle = FlatStyle.Flat;
                    b4.Image = TITOMS_LOGIN.Properties.Resources.icon2_1_;
                    b4.BackColor = Color.Transparent;

對於每個按鈕,它們顯示不同的形式

例如:按鈕 1 顯示表格 1 並隱藏其他

按鈕 2 顯示表格 2 並隱藏其他

您應該處理按鈕單擊事件,並且在每個事件中,您必須實例化所需的表單並使用 Show() 方法顯示它。

你有按鈕。
您需要按鈕的事件來執行操作。

b1.Click += new System.EventHandler(button1_Click);
b2.Click += new System.EventHandler(button2_Click);
b3.Click += new System.EventHandler(button3_Click);
b4.Click += new System.EventHandler(button4_Click);

button1_Click 是一種在單擊按鈕時調用的方法。

如果你想顯示一個新的表格:

 private void button1_Click(object sender, EventArgs e)
 {
     Form form = new Form();
     form.Show();
 }

如果要關閉其他 Forms,則 forms 必須是全局的。

    Form2 form1;
    Form2 form2;

    private void button1_Click(object sender, EventArgs e)
    {
        //Create new Form
        form2 = new Form2();
        form2.Show();

        //Check if other Form1 is not null -> it was initialized
        if (form1 != null )
        {
            form1.Close();
            form1.Dispose();
            form1 = null;
        }
    }

暫無
暫無

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

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