Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 719
ThN Avatar asked Dec 21 '22 06:12

ThN


1 Answers

This is affected by the ToolStrip's RenderMode setting. Only when you change it to System will the BackColor property have an effect. The other renderers use theme colors. You are probably not going to like System very much, but you can have you cake and eat it too by implementing your own renderer. Make it look similar to this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.toolStrip1.Renderer = new MyRenderer();
    }
    private class MyRenderer : ToolStripProfessionalRenderer {
        protected override void OnRenderLabelBackground(ToolStripItemRenderEventArgs e) {
            using (var brush = new SolidBrush(e.Item.BackColor)) {
                e.Graphics.FillRectangle(brush, new Rectangle(Point.Empty, e.Item.Size));
            }
        }
    }
}
like image 179
Hans Passant Avatar answered Dec 23 '22 18:12

Hans Passant