【问题标题】:Button creates a button then that button show/hide form按钮创建一个按钮,然后该按钮显示/隐藏表单
【发布时间】:2019-10-11 13:52:38
【问题描述】:

我已经知道如何在单击按钮时创建按钮。我应该在这行旁边写什么代码才能显示/隐藏表单?


                    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 显示 Form 1 并隐藏其他

按钮2显示Form 2并隐藏其他

【问题讨论】:

    标签: c# button show-hide


    【解决方案1】:

    您应该处理按钮单击事件,并且在每个事件中您必须实例化所需的表单并使用 Show() 方法显示它。

    【讨论】:

    • 你能举个例子吗?我是 C# 新手,不熟悉代码。
    【解决方案2】:

    你有按钮。
    您需要按钮的事件来执行操作。

    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();
     }
    

    如果要关闭其他表单,表单必须是全局的。

        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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-14
      • 2016-03-02
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多