C# Winforms | Form-border thickness

C4d picture C4d · Aug 20, 2015 · Viewed 11.4k times · Source

Is there any documentation of how thick a border of a regular form is?

The goal:
I've created a userControl with a width of 800px. I want to raise a popup (normal form in general) with a new instance of it at full resolution (800px - everything visible).

My Problem: Setting the form to Form.Size.Width = 800 wont do it. It looks like the border of the form is included in the width-property of the form. I need to subtract that border.

I should be something like: 2px + 800px + 2px

If you would like to see some code tell me, but I think its unnecessary in here.

EDIT:

enter image description here

After popping the control up:

enter image description here

Code for popup:

private void buttonPopup_Click(object sender, EventArgs e)
{
    Form MyPopup = new Form();
    customControl MyUserControl = new customControl();

    MyUserControl.Dock = DockStyle.Fill;

    Rectangle rc = MyUserControl.RectangleToScreen(MyUserControl.ClientRectangle);

    //int thickness = SystemInformation.Border3DSize.Width;
    //MyPopup.MaximumSize = new Size(MyUserControl.Size.Width + (thickness*2), 1500);

    MyPopup.Controls.Add(MyUserControl);
    MyPopup.MaximumSize = new Size(rc.Width, rc.Height);
    MyPopup.Show();
}

I mean your code looks logical to me. But still the result is the same. The userControl is displayed a tiny bit smaller. I know I've used dock = fill where my button isnt placed professionally inside the layout. But away from this there has to be a solution to just set the right size.

Answer

Dmitry Bychenko picture Dmitry Bychenko · Aug 20, 2015

It seems that your are looking for

int thickness = SystemInformation.Border3DSize;

another (and, INHO, a better one) possiblity is to use ClientRectangle of the control. For instance:

// Client rectangle in screen coordinates
Rectangle rc = MyControl.RectangleToScreen(MyControl.ClientRectangle);

// Let's align context menu (its width) to bottom of the control
MyContextMenuStrip.AutoSize = false;
// Depending on actual dropdown control you may want align either via
//   Width = rc.Width;
// Or 
//   ClientSize = new Size(rc.Width, someHeight);
MyContextMenuStrip.Width = rc.Width;

// Let's show context menu at the bottom of the control
MyContextMenuStrip.Show(new Point(rc.Left, rc.Bottom));