我在每行8行中有3个复选框.我希望每行中的第三个复选框只有在取消选中前两个复选框时才能进行检查.我不想为每一行写一个checkRow()方法.
最好的方法是什么?
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(); }
作为回应.
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; } }
我试图做这样的事情,所以我不必为每一行写checkRow()
您应该使用与所有三个委托的处理程序相同的方法.
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 }