سؤال

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,

هل كانت مفيدة؟

المحلول

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));
            }
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top