3

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.

Mehdi
  • 2,194
  • 2
  • 25
  • 39

2 Answers2

5

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);
    }
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Of course yes! We can paint anything via the 1st way. I use a color pallet in my app to determine the tool-strip items color, so I'm looking for a simple way to set some desired color as back-color of any arbitrary drop-down button. I tried the 2nd way, but it doesn't seem to have an effect on background! – Mehdi May 30 '12 at 09:37
  • +1 I'll mark your 1st approach as the answer if I cannot find another way. – Mehdi May 30 '12 at 10:18
0

Is the OnDropDownOpened event what you want?

private void toolStripDropDownButton_DropDownOpened(object sender, EventArgs e)
{
   toolStripDropDownButton.BackColor = Color.Red;
}
crdx
  • 1,412
  • 13
  • 26