0

I'm new to C#. The textbox1 is locked by default and I want to enable it the moment I check the checkbox1. I use the following code, but it doesn't work:

private void checkboxevent(object sender, EventArgs e)
{
    if (checkBox1.Checked = true)
        textBox1.Enabled = true;
    else
        textBox1.Enabled = false;
}
4
  • 2
    Winforms, WPF, Asp.NET or? Commented Jan 8, 2014 at 13:34
  • Cannot be WPF, in WPF it would be IsChecked. Commented Jan 8, 2014 at 13:35
  • checkBox1.Checked = textBox1.Enabled; Commented Jan 8, 2014 at 13:36
  • Winforms. checkBox1.Checked = textBox1.Enabled doesn't work too. Commented Jan 8, 2014 at 14:06

2 Answers 2

10

= is the assignment operator == is the equality operator

Take a close look at the following statement

if (checkBox1.Checked = true)
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure the event handler is getting called?
Probably not. When I put this code under a buttonclick, works fine. But I need to make it work at the moment the checkbox is unchecked.
Have you tried adding the eventhandler to the CheckStateChanged event of your checkbox?
No problem. Feel free to click the checkmark to indicate that this is the answer to your question.
0

add autopostaback="true" for your checkbox in order to refresh the page whenever the checkbox is checked or unchecked

I'm assuming that you're talking about webforms.

Also = true is incorrect. It should be == true

Also, == true is altogether redundant. if (checkbox.Checked) is all you need.

3 Comments

Could be WinForms, and they don't have AutoPostBack. :)
Yeah, and if it's Webforms, do you really wanna post back just to enable a text box. Better to use some jQuery
Agreed, but I am attempting to answer the question rather than propose a different approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.