1

I have 3 check boxes in each row of 8 total rows. I want to have the third checkbox in each row to get checked only when the first two checkboxes are unchecked. I do not want to write a checkRow() method for each row.

What is the best way to go about it?

private void checkRow()
{
    for (int i = 0; i < 8; i++)
    {
        var arraylist = new[] { checkbox1, checkbox2, checkbox3 };
        if (checkbox1.Checked || checkbox2.Checked)
        {
            arraylist[2].Checked = false;
        }
        else
            arraylist[2].Checked = true;
    }
}


private void checbox1_CheckedChanged(object sender, EventArgs e)
{
    checkRow();
}

private void checbox2_CheckedChanged(object sender, EventArgs e)
{
    checkRow();
}

private void checbox3_CheckedChanged(object sender, EventArgs e)
{
    checkRow();
}

In response.

private void checkRow()
{
    var arraylist = new[] { checkEdit1, checkEdit2, checkEdit3 };
    var arraylist1 = new[] { checkEdit4, checkEdit5, checkEdit6 };
    var arraylist2 = new[] { checkEdit7, checkEdit8, checkEdit9 };
    var array = new[] { arraylist, arraylist1, arraylist2 };

    for (int i = 0; i < 8; i++)
    {
        //if checkedit1 or checkedit2 is checked the checkedit3 should not be checked
        if (array[i]....Checked || array[i]....Checked)
        {
            arraylist[i]...Checked = false;
        }
        else
            arraylist[i]...Checked = true;
    }
}

I was trying to do something like this so that I dont have to write the checkRow() for each row

2
  • You know, you're not using i in your for loop... Commented Jan 7, 2009 at 3:13
  • Yes, I figured that was what you were getting at. Commented Jan 7, 2009 at 3:33

2 Answers 2

6

You should use the same method as the handler for all three delegates.

chkbox.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
chkbox2.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
chkbox3.CheckedChanged += new EventHandler(chkbox_CheckedChanged);

private void chkbox_CheckedChanged(object sender, EventArgs e)
{
    // do your stuff here
}
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you're not using a DataGridView or other way of organizing them into logical rows, why don't you do the following:

Store the checkboxes in an array so you have easy access to them.

  CheckBox[,] checkArray = new CheckBox[8,3]...

Store the row index in the Tag property of the first and second checkboxes.

  checkBox01.Tag = 0;
  checkBox02.Tag = 0;
  checkBox11.Tag = 1;
  checkBox12.Tag = 1;

Have all the first and second checkboxes point to the same event handler:

  checkBox01.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
  checkBox02.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
  checkBox11.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);
  checkBox12.CheckedChanged += new EventHandler(aCheckBox_CheckedChanged);

In the event handler, you now know exactly which check box to update and no longer have to loop:

  private void aCheckBox_CheckedChanged(object sender, EventArgs e)
  {
      int rowIndex = (int)((CheckBox)sender).Tag;
      checkArray[rowIndex,2].Checked = !(checkArray[rowIndex,0].Checked || 
                                         checkArray[rowIndex,1].Checked);
  }      

You can also do this using string lookups with the checkbox name, but it is surely slower and is a pain to refactor later if you choose to rename the checkboxes.

Comments

Your Answer

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