im making 10x10 cube with panels and labels inside to check how many times the cursor entered on the panel, changing the color on panel and and the label showing the number like 1, 2 ,3 ,4 etc. The colors are like this 1-5 is blue, 6-10 is green, 11-15 is yellow and 20 or more is red, My problem is when the cursor touch only the label; only the label change but the color on my panel dont change or my label background change color but the panel have other color.
I asked some similar before but only checking the color on my panels so this is the code: Old_Question:
private void panel_MouseEnter(object sender, MouseEventArgs e)
{
Control ctrl = sender as Control;
//get previous value from control tag or start at 0
int count = ctrl.Tag == null ? 0 : (int)ctrl.Tag;
//set backcolor of control based on tag number
if (count >= 20) ctrl.BackColor = Color.Red;
else if (count >= 15) ctrl.BackColor = Color.Yellow;
else if (count >= 10) ctrl.BackColor = Color.Lime;
else if (count >= 5) ctrl.BackColor = Color.Cyan;
else ctrl.BackColor = Color.SlateBlue;
ctrl.Tag = ++count;
}
Then, i modified the code to work with my labels.
private void panel_MouseEnter(object sender, EventArgs e)
{
Control ctrl = sender as Control;
Control lctrl = sender as Control;
//get previous value from control tag or start at 0
int count = ctrl.Tag == null ? 0 : (int)ctrl.Tag;
//set backcolor of control based on tag number
if (count >= 20) ctrl.BackColor = Color.Red;
else if (count >= 15) ctrl.BackColor = Color.Yellow;
else if (count >= 10) ctrl.BackColor = Color.Lime;
else if (count >= 5) ctrl.BackColor = Color.Cyan;
else ctrl.BackColor = Color.SlateBlue;
lctrl.Text = count.ToString();// count for my label
count++;
ctrl.Tag = count;
}
Note: I added on my label and my panel the same event.