문제

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