user1231969
user1231969

Reputation:

UserControl size changes after inserting in Form

I have a Windows Form class named UI which has initially no controls in it. There is a custom UserControl StartPage which has size 422, 117.

When I add StartPage in UI its size changes

UserControl c = new StartPage();
c.Dock = DockStyle.Top;          // Also tried DockStyle.Fill
// Size here: 422, 117

Controls.Add(c);
// Size here: 406, 117

What is happening and how do I prevent this behavior? I basically want the UI to automatically resize itself to contain StartPage without using AutoSize property.

Is there any standard way to achieve this behavior?

Upvotes: 1

Views: 3695

Answers (3)

Rajkumar S
Rajkumar S

Reputation: 114

Another solution is to change the AutoScaleMode to Inherit in the usercontrol. This takes care of the scaling issues. By default, it will have the value "Font".

AutoScaleMode = Inherit

Upvotes: 3

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

It happens because of the font, change the usercontrol font to the parent form font and everything will be load as expected.

Upvotes: 3

Andrey Korneyev
Andrey Korneyev

Reputation: 26856

You should increase your form width, currenctly it is to small - so usercontrol is being resized. You can do it in runtime like this:

UserControl c = new StartPage();
c.Dock = DockStyle.Top; 

Width = c.Width + Width - ClientSize.Width;
Height = c.Height + Height - ClientSize.Height;

Controls.Add(c);

Upvotes: 0

Related Questions