2

i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:

private void Register_Visitor_Load(object sender, EventArgs e)

On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:

textbox1.enabled = false;

i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:

CheckState state = checkBox1.CheckState;
            switch (state)
            {
                case CheckState.Checked:
                    {
                        textBox1.Enabled = true;
                        break;
                    }
                case CheckState.Indeterminate:
                case CheckState.Unchecked:
                    {
                        break;
                    }

now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!

2
  • try doing the code in the check changed event of check box. Commented Dec 12, 2013 at 6:43
  • make checked event of the checkbox. Commented Dec 12, 2013 at 6:43

5 Answers 5

10

You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled

private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
  textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Isn´t textBox1.Enabled = checkBox1.Checked; even better?
@Somachr: it will be if you don't have to deal with grayed states; since CheckState.Indeterminate has been mentioned in the question, I think it'll be safer to declare the state expilitly.
1

you are placing code at wrong event.

Instead of placing in pageload place that code on chekchange event of checkbox.

That will help you.

private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
        if (((CheckBox)sender).Checked)
        {
            textBox1.Enable=true;
        }
        else
        {
            textBox1.Enable=false;
        }
}

1 Comment

it works for me, thanks @C Sharper
0

Place the above code inside the function which handles the event for check box. In your case it is checkchanged status.

Comments

0

You can try this:

 private void checkBox1_Click(object sender, EventArgs e)
 {

        if (checkBox1.Checked)
        {
            textBox1.Enabled = false;
        }
        else
        {
            textBox1.Enabled = true;
        }
 }

Comments

0

I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
       button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {
            button1.Enabled = true;
        }
        else
        {
            button1.Enabled = false;
        }


    }

Comments

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.