Question

I need to change back-color of a ToolStripDropDownButton when its drop-down is opened. How can I do it?

I tried to inherit a class from the ToolStripProfessionalRenderer and then override the OnRenderDropDownButtonBackground, but it only affects when the drop-down is closed.

Was it helpful?

Solution

I believe you can use the following approaches:

1-st approach:

toolStripDropDownButton1.Paint += toolStripDropDownButton1_Paint;
//...
void toolStripDropDownButton1_Paint(object sender, PaintEventArgs e) {
    if(toolStripDropDownButton1.Pressed) {
        // TODO Paint your pressed button
        e.Graphics.FillRectangle(Brushes.Green, e.ClipRectangle);
    }
}

2-nd approach:

toolStrip.Renderer = new PressedRenderer();
//...    

class PressedRenderer : ToolStripProfessionalRenderer {
    protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs e) {
        if(e.Item.Pressed)
            e.Graphics.Clear(Color.Green);
        else base.OnRenderDropDownButtonBackground(e);
    }
}

OTHER TIPS

Is the OnDropDownOpened event what you want?

private void toolStripDropDownButton_DropDownOpened(object sender, EventArgs e)
{
   toolStripDropDownButton.BackColor = Color.Red;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top