There are 20 buttons and 20 checkboxes. The task is to make the button active by pressing the checkbox. Is it possible to do this easier than by creating two arrays to put down the same indices.
|
2 answers
It is possible for each checkbox in the Tag
property to insert a link to the corresponding button. Then sign all checkboxes for one click handler, in which to write something like this:
CheckBox checkBox = (CheckBox)sender; Button button = checkBox.Tag as Button; if (button != null) { button.Enabled = checkBox.Checked; }
|
In Tag checkboxes - number.
cb.CheckedChanged += (s,e) => { Button btn = this.Controls.OfType<Button>().FindByName("button" + cb.Tag.ToString()).Single(); btn.Enabled = (sender as CheckBox).Checked; }
|
for
loop? - VladD