lundi 14 octobre 2019

Dynamically generating Checkboxes but the event handler only works for the last one

Im trying to make dynamically checkboxes with a event handler but the event handler only works for the last one generated..

I have tried to change the position of my code. I have also tried to make more checboxes to se if that would make any difference.

for (int i = 0; i < appointments.TotalCount; i++)
            {
                lstChckBox = new List<CheckBox>();
                box = new CheckBox();
                box.Tag = i;
                box.Text = appointments.Items[i].Subject;
                box.AutoSize = true;

                box.Location = new Point(KalenderLbl.Location.X, KalenderLbl.Location.Y + KalenderLbl.Height + 5 + (i * 25));
                lstChckBox.Add(box);

                box.CheckedChanged += new EventHandler(chck_CheckedChanged);

                Controls.Add(box);
            }

        }


    void chck_CheckedChanged(object sender, EventArgs e)
        {
            foreach (CheckBox item in lstChckBox)
            {
                if (item.Checked == true)
                {
                    Hide();
                }
            }
        }

I want to know how to change the code so every checkbox have this event handler..




Aucun commentaire:

Enregistrer un commentaire