Controls.Add Magnifying UserControl !?!?!?!?!

  • Thread starter Thread starter eBob.com
  • Start date Start date
E

eBob.com

This is just driving me nuts. I have a really simple user control. It's
just a small textbox and small button. Created entirely in the designer.
No code. The size of the control is slightly less than 50x50. But after I
add it to a Panel, Panel.Controls.Add, it has a size of over 100x100 (but
less than the default user control size of 150,150). The textbox and button
are obviously about twice the size they should be. The object comes out of
the contructor the right size. In the next step I set the Location property
and after that the size is still OK. Then I add it to the panel and after
that the size has been corrupted and is over 100x100. If I change the code
to add a Label to the Panel, in place of the user control, that works fine.
If I add the user controls to the form, Me.Controls.Add, I have the same
problem. If I create a new but similar user control it has the same
problem.

I've created user controls before, but did I forget some step there?

How can a control have the right size immediately before calling
Controls.Add and the wrong size after that call?

Does anyone have any idea what might be going on?

Thanks, Bob
 
Try setting

myUserControl.Dock = DockStyle.None

before adding it to the collection

Cheers,
Johnny J.
 
Thank you Johnny, that sounds like a good idea. However, I've had no joy.
I tried setting Dock and Anchor to None after New'ing the user control and
that had no apparent effect. So then I also tried setting Dock and Anchor
to None in the constructor of the user control, but still no joy.

I'm wondering if something in the project might be corrupted, so I will not
try to re-create everything in a new project.

But if you have any other ideas I'd still be interested.

Thanks again, Bob
 
I have a thought: After adding it to the panel, the usercontrol and the
contained controls will probably inherit the Font property from the
container control (i.e. the panel).

If that font has a size that is larger than the textbox's original font
size, it could force the textbox to change size to accomodate the larger
font.

Could that be it?

/Johnny J.
 
Another good thought Johnny. Thanks. But after posting my last note I
created a new project, created a new user control from scratch (luckly it's
very simple), did some copying and pasting to get the form1 stuff, and this
new project works fine. So ... unlikely as it might seem, I have to
conclude that somehow something in the project got corrupted.

Thanks again, Bob
 
Back
Top