There is an exception in the form of the name of this portal, when debugging it runs endlessly inside the foreach with the 1st element, without going to the others. What is it connected with?

public void GenerateView(ref CheckBox cb) { CheckBox[] CB = new CheckBox[] { CBZPI,CBZFI,CBBrakM,CBBrakW }; foreach (CheckBox cbt in CB) { cbt.Checked = false; if (cbt == cb) cbt.Checked=true; } 
  • if (cbt == cb) will never be equal, what are you trying to compare? - pasha goroshko
  • Can I pass the Checkbox.Name parameter for comparison? void Foo (string name) {CheckBox [] CB = new Checkbox [] {...}; foreach (var cbt in CB) {cbt.Checked = cbt.Name == name; }} - XelaNimed pm
  • I transfer the link to CheckBox there? `private void CBZFI_CheckedChanged (object sender, EventArgs e) {CheckBox tb = (CheckBox) sender; GenerateView (ref tb); } `You can, but basically I want to understand how to work with objects. + even if so, and he would never be equal, which prevents him from moving to the next iteration, I have no while - Vadim Konovalov
  • How to write code normally in the comments?) - Vadim Konovalov
  • And, everything, in the course of it, I understood that line cbt.Checked = false; I call handler CBZFI_CheckedChanged. I did not know that this is how it works) - Vadim Konovalov

1 answer 1

If I understand correctly, you want to implement something like that ...

work example

Use the Tag property value to compare. For clarity, I create checkboxes in the code, but you are free to create them in the designer, just remember to assign the necessary values ​​for the Tag property to all checkboxes.

 public partial class Form1 : Form { private readonly string _tagDog = "Dog"; private readonly string _tagCat = "Cat"; public Form1() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; this.Text = "Пример"; //добавляем в коде чексбоксы AddTypeAnimalsCheckboxes(); AddAnimalsCheckboxes(); } private void AddTypeAnimalsCheckboxes() { int startY = 33; int delta = 20; var checkBoxDogs = new CheckBox { Name = "checkBoxDogs", Tag = _tagDog, //! Text = "Собаки", Height = 17, Width = 70, Location = new Point(x: 34, y: startY) }; var checkBoxCats = new CheckBox { Name = "checkBoxCats", Tag = _tagCat, //! Text = "Кошки", Height = 17, Width = 70, Location = new Point(x: 34, y: startY + delta) }; checkBoxDogs.CheckedChanged += CheckBoxAnimal_CheckedChanged; checkBoxCats.CheckedChanged += CheckBoxAnimal_CheckedChanged; groupBoxTypeAnimal.Controls.Add(checkBoxDogs); groupBoxTypeAnimal.Controls.Add(checkBoxCats); } private void AddAnimalsCheckboxes() { int startY = 33; int delta = 20; var checkBoxDog1 = new CheckBox { Name = "checkBoxDog1", Tag = _tagDog, //! Text = "Шарик", Height = 17, Width = 70, Location = new Point(x: 36, y: startY) }; var checkBoxCat1 = new CheckBox { Name = "checkBoxCat1", Tag = _tagCat, //! Text = "Мурзик", Height = 17, Width = 70, Location = new Point(x: 36, y: startY + delta) }; var checkBoxDog2 = new CheckBox { Name = "checkBoxDog2", Tag = _tagDog, Text = "Мухтар", Height = 17, Width = 70, Location = new Point(x: 36, y: startY + delta * 2) }; var checkBoxCat2 = new CheckBox { Name = "checkBoxCat2", Tag = _tagCat, Text = "Барсик", Height = 17, Width = 70, Location = new Point(x: 36, y: startY + delta * 3) }; groupBoxAnimals.Controls.Add(checkBoxDog1); groupBoxAnimals.Controls.Add(checkBoxCat1); groupBoxAnimals.Controls.Add(checkBoxDog2); groupBoxAnimals.Controls.Add(checkBoxCat2); } private void CheckBoxAnimal_CheckedChanged(object sender, EventArgs e) { CheckBox checkBox = sender as CheckBox; foreach (CheckBox cb in groupBoxAnimals.Controls.OfType<CheckBox>()) { //ищем по совпадению со значением Tag if (cb.Tag == checkBox.Tag) { cb.Checked = checkBox.Checked; } } } } 
  • Well, not exactly, I need to choose a cat, and at the same time all the dogs, donkeys and cows are gone. Well, your code is not difficult to change under it, thanks, I don’t understand why in my version the change of the "Checked" property is self-invoking the "CheckedChanged" event handler and because of this it loops, but not in yours? - Vadim Konovalov