在Form中插入后,UserControl大小会发生变化

时间:2014-09-16 10:27:21

标签: c# winforms

我有一个名为UI的Windows窗体类,它最初没有控件。有一个自定义UserControl StartPage,其大小为422, 117

当我在StartPage中添加UI时,其尺寸更改

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

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

发生了什么以及如何防止此行为?我基本上希望UI自动调整大小以包含StartPage而不使用AutoSize属性。

是否有任何标准方法可以实现此行为?

3 个答案:

答案 0 :(得分:1)

您应该增加表单宽度,当前它会变小 - 因此正在调整用户控件的大小。 你可以在运行时这样做:

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

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

Controls.Add(c);

答案 1 :(得分:1)

由于字体的原因,将usercontrol字体更改为父表单字体,所有内容都将按预期加载。

答案 2 :(得分:0)

另一种解决方案是将用户控件中的 AutoScaleMode 更改为 Inherit。这可以解决缩放问题。默认情况下,它将具有值“字体”。

AutoScaleMode = Inherit