Question

I'm using Windows 7 and Winforms/C#.

When I add a toolstrip to a toolstrip container, I get 3 pixels of space to the left of the toolstrip. Even if I set the location to 0,0, it resets it back to 3,0. Here is a picture to demonstrate this:

enter image description here

How can I avoid this? I've set the padding and the margin of the ToolStripContainer to 0, and experimented with different RenderModes, but nothing seems to help. Any ideas?

Was it helpful?

Solution

public Form1()
{
    InitializeComponent();
    this.toolStripContainer1.TopToolStripPanel.RowMargin = new Padding(0);
}

Just have to set the RowMargin property of the ToolStripPanels within the ToolStripContainer. It is not available in the designer so it needs to be set in code as seen above. Apparently by default, the Left property of that RowMargin is set to 3. That is why the location was not changing when you set it to 0.

Result:

ToolStripContainer with RowMargin of 0

EDIT: It seems I've sparked my own curiosity. Now I'm trying to see if I can get rid of the 2 pixel gap at the bottom. That one seems a lot harder to track down so far.

EDIT 2: Ok I figured it out. It was being done by the the ToolStrip's Renderer. In this example, I was using System as the RenderMode, so I added a new class in the project that inherits from ToolStripSystemRenderer and overrode the OnRenderToolStripBorder method and commented out the call to the base method.

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class MyToolStripSystemRenderer : ToolStripSystemRenderer
    {
        protected override void  OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            //base.OnRenderToolStripBorder(e);
        }
    }
}


Then I added another line in the form's constructor to assign the custom renderer.

public Form1()
{
    InitializeComponent();
    this.toolStrip1.Renderer = new MyToolStripSystemRenderer();
    this.toolStripContainer1.TopToolStripPanel.RowMargin = new Padding(0);
}


Result:
ToolStrip panel with RowMargin of 0 and no 2 pixel border

Edit 3: So I found out today this is a known bug with ToolStripSystemRenderer. I suppose I didn't find it because I was initially thinking it had something to do with the ToolStripContainer. Anyway, for those who it helps, here's a SO question regarding the "border" of the toolstrip. https://stackoverflow.com/a/2060360/2056131

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top