How can I remove the white line drawn under a MenuStrip control?
Asked Answered
H

3

8

I read a few other articles about how people want to customize the colors and gradients of a MenuStrip.

What I want to do is remove the gradient so that the MenuStrip is the same color as the rest of the form which, for me, is the default settings used when creating a new WinForms project. I tried changing the RenderMode to 'System' and it works sort of, but it leaves a white line the length of the MenuStrip when I build and run it. Do I have to do some drawing and painting? Or is there an easier way?

Hypogeous answered 16/2, 2011 at 3:5 Comment(0)
S
9

This is basically the same question as this one

The answer references this Microsoft bug post

It seems to be an issue all the way from 2005. Although the comments say that it is a MS bug which will not be fixed, there is a workaround which involves implementing your own renderer:

public class MySR : ToolStripSystemRenderer
{
    public MySR()
    {
    }

    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        //base.OnRenderToolStripBorder(e);
    }
}

Then all you have to do is set your menustrip's renderer to the one you just implemented:

menustrip1.Renderer = new MySR();

I just tried it out and it seems to work just fine.

Steven answered 16/2, 2011 at 3:45 Comment(2)
Indeed a duplicate question, but your answer here is much better. Mainly because you took the time to copy and paste the workaround code.Perce
It's pretty useless of MS for this bug to be ~6 years and 2 versions later and its still not fixed! Thanks for your answer Yetti, I appreciate it.Hypogeous
C
3

I agree with Yetti but if you wish to maintain your borders you can try this. Replace Brush with your background Color

protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
  base.OnRenderToolStripBorder(e);
  e.Graphics.FillRectangle(Brushes.Black, e.ConnectedArea);
}
Calondra answered 7/11, 2011 at 20:29 Comment(2)
You could probably just use Brushes.Black instead of creating a new black brush, which you aren't disposing in your example.Apothegm
Thank you. I didn't know you could do that. I edited the above answer.Calondra
M
0

@Yetti's answer is correct, but has a small issue. It also removes the border from the dropdown part of the menu (the one that opens when you click on an item). In order to avoid this, do the following in OnRenderToolStripBorder:

public class MySR : ToolStripSystemRenderer
{
    public MySR()
    {
    }
    
    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        if (e.AffectedBounds == e.ToolStrip.Bounds) return;
        base.OnRenderToolStripBorder(e);
    }
}
Manservant answered 24/11, 2023 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.