views:

193

answers:

2

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) Add 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?

+1  A: 

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.

stakx
Hi, it has to be a System.Windows.Forms.UserControl, not just a normal control.
Jules
I've amended my question to clarify
Jules
Damn I hadn't seen your edit, it appears to work! I'd originally hard coded AutoScaleMode = Font into the usercontrol because that's what the designer does and I was having probs before. I'll do some further testing and hope my previous problem doesn't occur. Any idea why it works?
Jules
Well, I knew there was a reason I'd changed it to an explicit AutoScaleMode = Font. If it's set at inherit, it works for the case I describe in the OP, except when the container form is initially set to a non-default font. My solution is to set the AutoScaleMode to Font, then after the control is added to a form, I set it to Inherit. It will then scale correctly if the font is changed at run-time.
Jules
+1  A: 

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);
Hans Passant
@nobugz: To clarify, If I set the Font on my form to what I desire and leave the Font setting on Default on my `UserControls` they will "inherit" my Parents Font setting? I don't have to do or set anything else? `AutoScale = Font` or something?
Refracted Paladin
Yes, that's how ambient properties work.
Hans Passant
Thank you, I have, somehow, never heard that before.
Refracted Paladin
No it's not that. I haven't touched the properties of the usercontrol. All I did was create it, colour the background, so I could see it, and add a few buttons. The scaling is happening because the controls In the usercontrol scale correctly. It's The usercontrol itself that is growing too large. Perhaps it's something to do with VB?
Jules
The best way to see it is to put a button in the bottom-right hand corner of the usercontrol (leave the anchor at default). After increasing the font size, the gap between to button and the edge of the usercontrol is far too big.
Jules
@Jules, nobody seems to be able to repro your problem. Get ahead by posting a repro project on a file sharing service.
Hans Passant
I've add links to a vb.net and c# sample. Thanks for your time.
Jules
@Jules, solution in my updated post, below the line.
Hans Passant
No, that doesn't work either. I had tried that on the off chance. Are you sure you didn't try that with the code before the call to InitializeComponent?
Jules
I hadn't seen stakx edited answer above. This appears to work, just doing some further testing ...
Jules