简体   繁体   中英

C# Label highlight and remove highlight

I'm sure this is something very easy to figure out but I cannot do it. I have a winform with 3 Label inside a Panel . When the form loads, the first Label has a Paint event that draws a rectangle on it. I would like a backgroundWorker to go through each one, wait 5 seconds, restore the Label to normal (redrawing I'm guessing) and then draw a rectangle on the following Label .

public List<Label> GetLabelList()
        {
            return new List<List>()
            {
                label1,
                label2,
                label3,
                label4
            };
        }

private void bgBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        var getList = GetLabelList();

        for (int i = 0; i < getList.Count; i++)
        {
            if ((bgBackgroundWorker.CancellationPending == true))
            {
                e.Cancel = true;
                break;
            }

            else
            {
                Thread.Sleep(5000);
                getList [i].Paint += RemoveLabelHighlight;
                getList [i].Invalidate();

                if (i < 2)
                {
                    getList [i + 1].Paint += AddLabelHighlight;
                    getList [i + 1].Invalidate();
                }

                bgBackgroundWorker.ReportProgress((i * 10));
            }
        }
    }

private void AddLabelHighlight(object sender, PaintEventArgs e)
    {
        var label = sender as Label;
        e.Graphics.DrawRectangle(new Pen(Color.DeepPink, 8), label.ClientRectangle);
    }

    private void RemoveLabelHighlight(object sender, PaintEventArgs e)
    {
        var label = sender as Label; 
        e.Graphics.DrawRectangle(new Pen(Color.Green, 8), label.ClientRectangle); // This should return the Label back to original state
    }

This works but when the rectangle is drawn, the label is cut off all the way around. Any suggestions?

Also, I'm sure there is a much better and more efficient way to achieve this, maybe by an EventHandler or something. I'd like some suggestions, if possible.

This is actually being caused by your use of the pen width of 8 pixels, I believe. Try a different size and see if that changes the size of the rectangle not being drawn.

To fill the rectangle instead, use:

e.Graphics.FillRectangle(new SolidBrush(Color.DeepPink), e.ClipRectangle);

EDIT Since you're now completely responsible for drawing the control, the text can be redrawn with a DrawString call:

e.Graphics.DrawString(label.Text, label.Font, SystemBrushes.ControlText, new PointF(0,0));

EDIT Here's how to nest a panel and a label to achieve what you're looking for:

  • Add a new panel, set the padding to 8,8,8,8, and BackColor to whatever you like
  • Add a new label to this panel, set it's AutoSize property to false, Dock property to Fill, and TextAlign property to MiddleCenter

While I have always loved doing owner-drawn stuff, sometimes it's just easier to use what's there! For fun though, I would wrap this into a new Panel-derived control to make it easy to reuse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM