'Why doesn't toolstriplabel's backcolor property change during design time or run time?

I need to have a toolstrip label and its back color changed during runtime, but no matter what I do. It just won't change its backcolor, even though they give option to change its backcolor. Why is that and how do you get its backcolor property to change during runtime or design time?

Thanks in advance,



Solution 1:[1]

I did not like changing the RenderMode so I went with this:

private static void SetBackgroundColor(ToolStripLabel tsl, Color c)
    {
        if (tsl != null)
        {
            Bitmap bm = new Bitmap(1, 1);
            using (Graphics g = Graphics.FromImage(bm)) { g.FillRectangle(new SolidBrush(c), 0, 0, 1, 1); }
            tsl.BackgroundImageLayout = ImageLayout.Stretch;
            Image oldImage = tsl.BackgroundImage;
            tsl.BackgroundImage = (Image)bm.Clone();
            if (oldImage != null) { oldImage.Dispose(); }
            bm.Dispose();
        }
    }

Uses BackgroundImage to fill in solid color bitmaps.

You could probably attach to the BackColorChanged event and call this function.

Solution 2:[2]

Good idea, @Plater. Thanks. I tried to make your code even simpler and it works for me :-)

ToolStripLabel.BackgroundImageLayout = ImageLayout.Stretch
ToolStripLabel.BackgroundImage = New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(ToolStripLabel.BackgroundImage)
g.Clear(Color.PaleGreen)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Plater
Solution 2 MK.