0

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.

Pictures about: enter image description here enter image description here

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.

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
  • why don't you limit your conditions. . .if (count >= 20) ctrl.BackColor = Color.Red; else if (count >= 15 && count <20) ctrl.BackColor = Color.Yellow; else if (count >= 10 && count < 15) ctrl.BackColor = Color.Lime; else if (count >= 5 and count < 10) ctrl.BackColor = Color.Cyan; else ctrl.BackColor = Color.SlateBlue; – Mowazzem Hosen Sep 17 '19 at 00:45
  • Because i don't think is important... – Javier Aceves Sep 17 '19 at 00:51
  • Show screenshot please? – TerribleDog Sep 17 '19 at 00:54
  • @TerribleDog i update the post – Javier Aceves Sep 17 '19 at 01:38
  • 2
    If you would use only labels and no panels, the code should be simpler and use less resources. – Phil1970 Sep 17 '19 at 02:02
  • How about `label_mouseEnter` instead of panel because the the label is in front of the panel in design, and also make the `label dock=fill`, `autosize=false` to cover all the the size of panel – Cyrille Con Morales Sep 17 '19 at 02:04
  • @Javier I removed my answer but I understand your problem now. You need both the Panel and Label to update regardless who MouseEnter event is triggered. Well the Label is easy, you just set the properties on its Parent control which should be a Panel control. The reverse is a little harder. You need to find Panel child control to set the properties of the Label. To do this you must search through the Panel list of child controls to find the Label control that need to be updated. I can provide an example later if you need more information. – Mentat Sep 17 '19 at 02:08
  • @CyrilleConMorales, i tried that and not work good because if number is higher to 100 the label only show two first number. – Javier Aceves Sep 17 '19 at 02:14
  • @JavierAceves, I think you have to increase the width of the label to show three digits – Venkataraman R Sep 17 '19 at 03:45
  • Using only Labels is a good idea but if you want to stick to the setup you have all you need to do is pass on the mouse event from the label to its panel. – TaW Sep 17 '19 at 09:05
  • yeah i used the labels but when reach the number 100 the label move the number and only show the two first numbers – Javier Aceves Sep 17 '19 at 12:11
  • @JavierAceves Having a label inside a panel creates a bigger problems. Lets examine a hypothetic situation where the problem occurs. Move the cursor from panel to label and back. Those events will fire:panel leave->label enter, label leave->panel enter....etc. How can you assure that you are at the same square and dont update the count value?? – γηράσκω δ' αεί πολλά διδασκόμε Sep 17 '19 at 13:28
  • excellent question, that is why i have the problem when the cursor only enter in the label, my panel can't update – Javier Aceves Sep 17 '19 at 15:52

1 Answers1

1

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.

Mentat
  • 356
  • 1
  • 5
  • On your `p. = ` i need add the "if colors"?, im confused – Javier Aceves Sep 17 '19 at 16:10
  • Yes, that is correct. I did not want to give the full answer, just the parts you needed help with. The places where I put stuff in “<>” or where the comment start with “Place….” Is where you need to add your logic. I have updated the code to the full working version. – Mentat Sep 17 '19 at 17:49
  • Oh don't worry and thank you!, your code work very god! – Javier Aceves Sep 17 '19 at 22:22