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.

  • WPF or WinForms? Or maybe Silverlight? Asp? In the framework of the frameworks its own technique. Actually, maybe you just need a for loop? - VladD
  • WinForms. How in the for loop to make the name of the control? buttoni, where i is the loop variable - demmy
  • one
    And why do you need a name, sorry? I hope you generate all 20 pairs of controls, too, by loop, and not manually? - VladD
  • No, they are statically placed on the form. - demmy
  • one
    @VladD for the sake of justice to write the same thing many times - it’s not Hindu, it’s Chinese code (I don’t know why Chinese, maybe it’s just that). Hindu is more cool) - DreamChild

2 answers 2

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; }