【问题标题】:Hide and Show Second form on Button Click单击按钮时隐藏和显示第二个表单
【发布时间】:2015-06-27 10:46:32
【问题描述】:

我想通过单击按钮显示另一个表单(Form2)。基本上,当在 Form1 中单击按钮时,应该显示另一个 form(Form2),但这不应该隐藏 Form1,并且应该在 Form1 中将按钮文本更改为“隐藏进度”。当再次单击此按钮时,Form2 应隐藏并且按钮中的文本应更改为“显示进度”。

以下是我为完成这项工作所做的努力。当我单击“显示进度”按钮时,它会带来 Form2 并更改按钮中的文本。但是当我再次单击该按钮而不是隐藏 Form2 时,它会打开另一个 Form2 实例。

可能背后的原因是,布尔值没有保存。

这是我的按钮事件处理程序代码。

public partial class Main : Form
    {
       public string output_green, output_grey, output_blue, output_black;
       public bool visible;
 private void button1_Click(object sender, EventArgs e)
        {

            output progressWindow = new output();

            if (visible == false)
            {
                progressWindow.Show();
                button1.Text = "Hide Progress";
                visible = true;
            }

            else
            {
                progressWindow.Show();
                button1.Text = "Show Progress";
                visible = false;

            }

        }
}

我怎样才能实现我需要的。

【问题讨论】:

    标签: c# winforms visual-studio show-hide


    【解决方案1】:

    问题:

    每次单击button1 时,都会初始化一个进度窗口。

    另外,您在 else 部分使用 progressWindow.Show() 而不是 Hide()

    解决方案:

    button1_Click 中声明progressWindow。然后从button1_Click 初始化它。现在它只会被初始化一次(使用if)。

    output progressWindow = null;
    private void button1_Click(object sender, EventArgs e)
    {            
            if(progressWindow == null)
                progressWindow = new output();
            if (button1.Text == "Show Progress")
            {
                progressWindow.Show();
                button1.Text = "Hide Progress";
            }
            else
            {
                progressWindow.Hide();
                button1.Text = "Show Progress";
            }
        }
    }
    

    【讨论】:

    • @Raging Bull,它仍然没有隐藏Form2
    • 是的,您的解决方案仅初始化 Form2 一次,但单击按钮时不会隐藏 Form2
    • @AmritSharma:您也在其他部分使用progressWindow.Show()。这就是问题所在。现在我更新了我的答案。立即尝试。
    • 它工作得很好,现在我明白了我错了。感谢愤怒
    • 我真的不喜欢最后的编辑。您现在使用按钮的Text 属性作为信息存储——这绝不是一个好主意。一种属性,一种目的。额外的布尔值不是必需的,但它比将信息存储在不属于它的位置更好。
    【解决方案2】:

    对于进度窗口的生命周期与主窗体保持一致的较短解决方案:

        output progressWindow = new output();
        private void button1_Click(object sender, EventArgs e)
        {
            progressWindow.Visible = !progressWindow.Visible;
            button1.Text = (progressWindow.Visible) ? "Hide Progress" : "Show Progress";
        }
    

    在这里你不需要额外的布尔值,因为进度表本身完全能够告诉你它是否可见。

    【讨论】:

      【解决方案3】:
        // Creates a single instance only it it is request.
        private Output ProgressWindow
          {
            get 
                {
                    return progressWindow?? (progressWindow= new Output(){Visible = false};
                }
          }
      
         private Output progressWindow;
      
          private void button1_Click(object sender, EventArgs e)
          {            
               ProgressWindow.Visible = !ProgressWindow.Visible;
               button1.Text = (ProgressWindow.Visible) ? "Hide Progress" : "Show Progress";
      
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-06
        • 1970-01-01
        • 2013-01-16
        • 2016-05-21
        • 2012-05-06
        相关资源
        最近更新 更多