通过单击按钮来更改控件的可见性

问题描述:

我正在使用c#中的项目工作。我的窗体中有一个文本框,下面有一个按钮。我想通过单击按钮来更改我的文本框的可见性。例如,文本框在表单加载时隐藏。当用户单击按钮时,文本框出现在窗体上,然后用户再次单击它,文本框再次隐藏,如果我应该这样做,请帮助我。通过单击按钮来更改控件的可见性

感谢您的帮助。

+0

使用布尔和改变布尔按钮时被点击,如果布尔值为true,则使用按钮的.V来改变按钮的可见性有形财产。 – Max 2013-04-09 12:25:58

bool showtext = false; 

    public Form1() 
    { 
     InitializeComponent(); 
     textBox1.Visible = showtext; 
     button1.Click += button1_Click; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     showtext = !showtext; 
     textBox1.Visible = showtext; 
    } 
+1

如果您始终将其设置为true,则永远无法取消隐藏文本框。 – 2013-04-09 12:27:15

+1

打字速度比我想象的要快。 – 2013-04-09 12:30:46

一些像这样的事情应该这样做

 private void button1_Click(object sender, EventArgs e) 
     { 
      tbProgress.Visible = !tbProgress.Visible; 
     } 

private void button1_Click(object sender, EventArgs e) 
{ 
    textBox1.Visible = !textBox1.Visible; 
} 

public partial class Form1 : Form 
    { 
     bool buttonvisible = false; 

     public Form1() 
     { 
      InitializeComponent(); 
      button1.Visible = false; 
      button1.Click += button1_Click; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if(buttonvisible) 
      { 
       buttonvisible = false; 
       button1.Visible = false; 
      } 
      else 
      { 
       buttonvisible = true; 
       button1.Visible = true; 
      } 
     } 
    } 

或简单的,但不太编辑:

private void button1_Click(object sender, EventArgs e) 
{ 
    button1.Visible = !button1.Visible; 
} 
+0

tnx很多我的朋友 – 2013-04-09 19:48:14

+0

选择它作为答案,当你用我的答案:) – Max 2013-04-09 21:15:23