Reputation: 47
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.
Upvotes: 0
Views: 259
Reputation: 381
Javier Aceves,
The issue is that both controls need to be updated at the same time when a MouseEnter event is trigger. To accomplish this, you need to do two things. First, as mention by others, both the Panel and Label control needs to call a common MouseEnter event handler routine. Second, you need to update both the sender who triggered the event along with the sender's Parent or Child control depending on whether the sender is a Panel or Label.
Here is an Example on how to accomplish this:
private void Process_MouseEnter(object sender, EventArgs e)
{
int count = 0;
Panel p = null;
if (sender is Panel)
{
p = sender as Panel;
// Compute the count using data stored in Label.Tag
foreach (Control c in p.Controls)
if (c is Label)
{
Label l = c as Label;
l.Text = (((int)l.Tag) + 1).ToString();
l.Tag = (int)l.Tag + 1;
count = (int)l.Tag;
break;
}
//set backcolor of control based on tag number
if (count >= 20) p.BackColor = Color.Red;
else if (count >= 15) p.BackColor = Color.Yellow;
else if (count >= 10) p.BackColor = Color.Lime;
else if (count >= 5) p.BackColor = Color.Cyan;
else p.BackColor = Color.SlateBlue;
} /* end Panel if */
if (sender is Label)
{
Label l = sender as Label;
l.Text = (((int)l.Tag) + 1).ToString();
l.Tag = (int)l.Tag + 1;
count = (int)l.Tag;
//set backcolor of control based on tag number
p = l.Parent as Panel;
if (count >= 20) p.BackColor = Color.Red;
else if (count >= 15) p.BackColor = Color.Yellow;
else if (count >= 10) p.BackColor = Color.Lime;
else if (count >= 5) p.BackColor = Color.Cyan;
else p.BackColor = Color.SlateBlue;
} /* end Label if */
}
Note this code can be improved by incorporating the ideas mention by others.
For starters, if the Panel is your idea and not part of some requirement, as Phil1970 mentions, your code will be a lot simpler if you did not use Panels. Also, to solve the displaying of numbers problem researching the use of the LABEL’s AutoElipsis and AutoSize properties might help.
Upvotes: 1