kurupt_89
kurupt_89

Reputation: 1592

c# checkbox problem

I want to minimize the amount of code i have to write for this small problem. I have 1 textbox that has a relationship with 2 checkboxes as yes and no. The textbox on form load is set to disabled. When the yes checkbox is changed this event occurs -

private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
    textBox14.Enabled = true;
    checkNo1_cbx.Checked = false;
}

and when the no checkbox is changed -

private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
    textBox14.Enabled = false;
    checkYes1_cbx.Checked = false;
}

Although another problem is that i have to press yes twice to get it to check.

This is for a question on a form and so far it goes up to 11 questions and more will be added in the future. So my 2 problems so far is -

  1. How can I fix the problem when the checkbox is changed I have to press it again to check it.

  2. Is it possible to improve this code to minimize the amount of code i will have to write in the future.

Upvotes: 1

Views: 1760

Answers (4)

Scott Munro
Scott Munro

Reputation: 13606

I agree with Yahia. If you do need to explicitly provide the two options though, then you should consider using RadioButtons.

Upvotes: 0

szeliga
szeliga

Reputation: 621

unless there is a reason that you are making a round trip back and forth to the server to disabled a textbox on a checkbox selection change, why don't you just do that all on the client side via javascript?

Upvotes: 0

Yahia
Yahia

Reputation: 70369

Why are you using 2 checkoboxes ? One checkbox (check1) would be enough:

private void check1_CheckedChanged(object sender, EventArgs e)
{
    textBox14.Enabled = check1.Checked;
}

EDIT: Assuming that each question mean 1 textbox, then you need 1 checkbox per textbox... this could be further improved by using a more complex approach

Upvotes: 2

Jalal Said
Jalal Said

Reputation: 16162

private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
    OnCheck(true);
}
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
    OnCheck(false);
}

private void OnCheck(bool yes)
{
    textBox14.Enabled = yes;
    checkNo1_cbx.CheckedChanged -= checkNo1_cbx_CheckedChanged;
    checkNo2_cbx.CheckedChanged -= checkYes1_CheckedChanged;
    checkNo1_cbx.Checked = !yes;
    checkNo2_cbx.Checked = yes;
    checkNo1_cbx.CheckedChanged += checkNo1_cbx_CheckedChanged;
    checkNo2_cbx.CheckedChanged += checkYes1_CheckedChanged;
}

However consider using RadioBox instead of CheckBox because you want to if one being checked uncheck the other..

Edit: In your previous design, you get it wrong changed I have to press it again to check it because of you have two event handlers assigned to each of the check boxes. now at your code when the first one checked, you are disable the text box and make the other unchecked, but when you call the other unchecked Checked = false you are calling the second check box event handler also so it will enable the text and make the first one disable... you should remove the event handler by -= when updating at your code if you don't want the handler handler to be triggered again.. And that what I am doing in the code sample provided.

Upvotes: 2

Related Questions