Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the border padding on container controls in WinForms?

I set margin and padding to 0 0 0 0 but that doesn't have any effect for my TabControls. Look:

enter image description here

Here is what I am talking about. I want to stick the borders together.

How can I do this?

@Henk Holterman - yes, what's wrong with it ?

like image 479
cnd Avatar asked Feb 11 '11 11:02

cnd


2 Answers

There's a comment left in the source code for TabPage by an exasperated Microsoft programmer (edited to fit the page):

//HACK: to ensure that the tabpage draws correctly (the border will get 
//  clipped and gradient fill will match correctly with the tabcontrol).
//  Unfortunately, there is no good way to determine the padding used 
//  on the tabpage.
//  I would like to use the following below, but GetMargins is busted 
//  in the theming API:
//VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal);
//Padding themePadding = visualStyleRenderer.GetMargins(e.Graphics, MarginProperty.ContentMargins);

Visual Styles have been a major bug factory, particularly so for TabControl. Check this answer for a way to selectively turn it off for the TabControl so you'll get the behavior you are used to. Of course it does change the appearance.

like image 125
Hans Passant Avatar answered Oct 05 '22 23:10

Hans Passant


I agree with Henk. There's a border of the same size (9 pixels to my recollection) all the way around the container control. The reason it's there is to prevent you from squashing controls up too close to the edge. If you did that at the top, your control would be far too close to the tab headers at the top. It would look silly and confuse the user. WinForms is saving you from yourself here, and you don't even know it. Exactly the reason it was done in the first place.

Familiarize yourself with Microsoft's standard user interface guidelines, specifically the section on layout. Notice how all of the controls (the dialog box window itself, the tab control, etc.) have a border around them? It's 7 dialog units in the Visual C++ Resource Editor; WinForms uses a pixel specification.

    sample tab control, with border around edges
    spacing around a button control

like image 22
Cody Gray Avatar answered Oct 05 '22 23:10

Cody Gray