Your loop completion should be i < 4 , not i <= 4 , since your array has only 4 elements. Also boxes[i].Checked == true is redundant, you can just say boxes[i].Checked . boxes[i].Checked .
If you want to display checked flags when switching state, you need to add an event handler to them (for processing CheckBox.CheckChanged ):
public partial class Form1 : Form { public Form1() { InitializeComponent(); _checkBoxes = new CheckBox[] { _checkBox1, _checkBox2, _checkBox3, _checkBox4 }; foreach (var checkBox in _checkBoxes) checkBox.CheckedChanged += new EventHandler(ShowCheckedCheckboxes); } void ShowCheckedCheckboxes(object sender, EventArgs e) { string message = string.Empty; for (int i = 0; i < _checkBoxes.Length; i++) { if (_checkBoxes[i].Checked && _checkBoxes[i].Enabled) { message += string.Format("boxes[{0}] is clicked\n", i); } } MessageBox.Show(message); } CheckBox[] _checkBoxes; }
source share