samedi 13 mars 2021

CheckBoxes coloring by checking (Windows Forms)

I have three colored CheckBoxes. Each checkbox should be marked by checking. But when I move the mouse over the checkboxes, all checkboxes are selected in a monotonous color. How should I change the code (see below)? Thanks in advance!

private CheckBox[] checkboxes;

private void Form5_Load(object sender, EventArgs e)
{
   checkboxes = new[] {checkBox1, checkBox2, checkBox3 };

   foreach(var CK in checkboxes)
   {
     CK.Paint += new PaintEventHandler(CK_Paint);
   }
}

private void CK_Paint(object sender, PaintEventArgs e)
{
   if (checkBox1.Checked)
   {
       e.Graphics.FillRectangle(Brushes.Red, 0, 0, 13, 13);
   }
   if (checkBox2.Checked)
   {
      e.Graphics.FillRectangle(Brushes.Green, 0, 0, 13, 13);
   }
   if (checkBox3.Checked)
   {
      e.Graphics.FillRectangle(Brushes.Yellow, 0, 0, 13, 13);
            
   }

}





Aucun commentaire:

Enregistrer un commentaire