Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How could you make a text box visible after checking a checkbox?

How do you improve the code below?
if(checkbox1.Checked)
{
textbox1.Visible = true;
}
else
{
textbox1.Visible= false;
}

Where do I put the code to make the textbox visible after checking the textbox?
Posted
Updated 1-Jul-11 0:04am
v4

Hello,

Adding to the previous answers...

You could make it even shorter: :)

C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    TextBox1.Visible = CheckBox1.Checked;
}



Valery.
 
Share this answer
 
Select your checkbox, click Events and double-click on the:

CheckStateChanged

This will create the event handler. Enter your code inside it. It should look like:

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
   if(checkbox1.Checked)
   {
      textbox1.Visible = true;
   }
   else
   {
      textbox1.Visible= false;
   }
}
 
Share this answer
 
v2
Set AutoPostback of checkbox true.
And write your code in OnCheckedChanged
 
Share this answer
 
C#
set CheckBox1's autopostback property true
protected void Page_Load(object sender, EventArgs e)
       {
           TextBox1.Visible = false;
           if (CheckBox1.Checked == true)
           {
               TextBox1.Visible = true;
           }
           else
           {
               TextBox1.Visible = false;
           }
       }
 
Share this answer
 
v2
C#
textbox.Visible = true;
 
Share this answer
 
Comments
wolfsor 1-Jul-11 5:46am    
Where do I put the code to make the textbox visible after checking the textbox?
#realJSOP 1-Jul-11 7:28am    
In your original question, you asked *how* to set a textbox to be visible, not *where*. You do it in the Clicked or CheckChanged event handlers for the checkbox. Now, as to your vote of 2, I request that you change it to reflect that I did, in fact, answer your original question correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900