开发者

Problem with usercontrol scaling when added at run-time in .Net, WinForms

Here's a link to a tiny VB.Net 2008 sample:

http://www.4shared.com/file/255391716/26c45c1e/UserControlTest.html

And, a C# sample:

http://www.4shared.com/file/255392906/27f4efbb/UserControlTestCHash.html

Apologies but its a free hoster, so you have to wait 10 seconds.

First here's the steps to replicate, then I'll explain what the problem is:

(1) Create a System.Windows.Forms.UserControl and add a button to the bottom-right hand corner. Leave the button anchor as default (top-left). Add some more buttons dotted around so that you can see that they scale correctly.

(2) A开发者_StackOverflow社区dd the UserControl to a form in the construtor, after the InitializeComponent call.

(3) Run the form.

(4) Increase the form font size some way (eg click a form button).

All the controls within the usercontrol scale perfectly but the usercontrol itself doesn't. It's width and height are increased by way too much. Look at the margin now between the button at the bottom-right hand corner and the usercontrol.

To correct the problem, the usercontrol must be added before the InitializeComponent call.

If it wasn't possible for me to add the usercontrol before InitializeComponent, is there any way for me to correct the scaling?


Edit: I've deleted my previous answer (which basically said "works fine on my machine") and replaced it with the following.

On your custom UserControl, set AutoScaleMode = Inherit. This should correct the excessive scaling of the user control.


No repro, it scales perfectly. It will not scale properly if you've set the Font property of the UserControl and it doesn't match the font size of the form.

The Font property is an "ambient" property, like ForeColor and BackColor. As long as you leave it to the default setting (not bold in the Properties window) then it will use the Font of the parent. When every control uses the same font size, they'll all scale proportionally.

To fix, select the text of the Font property in the Properties window, press Backspace to erase it and press Enter. It resets back to the default.


Fix your code like this:

        this.SuspendLayout();
        UserControl1 uc = new UserControl1();
        this.Controls.Add(uc);
        this.ResumeLayout(false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜