Automatically Resize Form?

Z

zacks

I am developing an application in VS2005. I have a form that has a
PictureBox control whose purpose is to be able to plop a User Control
in it. And the User Control can be one of several possible User
Controls and these User Controls are all different in size, some
larger that the picture box it will be added to.

How do I get the form to resize when I add a User Control to the
picture box control when the User Control is larger than the picture
box?
 
K

kimiraikkonen

I am developing an application in VS2005. I have a form that has a
PictureBox control whose purpose is to be able to plop a User Control
in it. And the User Control can be one of several possible User
Controls and these User Controls are all different in size, some
larger that the picture box it will be added to.

How do I get the form to resize when I add a User Control to the
picture box control when the User Control is larger than the picture
box?

If you're not talking about fitting picturebox on a form which that's
higher on screen / desktop resolution (which i'd like to accomplish),
and you might have mentioned about a kind of alignment / object
fitting issue, i think you should do:

Picurebox1.size = <User_control>.size ' Assuming user control
providing a GUI and size info.
' Apply the same for the form

Me.size = Picturebox1.size ' Or add some space "Picturebox1.size + 50"

Hope this helps.
 
K

kimiraikkonen

If you're not talking about fitting picturebox on a form which that's
higher on screen / desktop resolution (which i'd like to accomplish),
and you might have mentioned about a kind of alignment / object
fitting issue, i think you should do:

Picurebox1.size = <User_control>.size ' Assuming user control
providing a GUI and size info.
' Apply the same for the form

Me.size = Picturebox1.size ' Or add some space "Picturebox1.size + 50"

Hope this helps.

Note: Of course do this when you need it, so put them in a IF....Then
statment block.
 
P

Peter Duniho

I am developing an application in VS2005. I have a form that has a
PictureBox control whose purpose is to be able to plop a User Control
in it. And the User Control can be one of several possible User
Controls and these User Controls are all different in size, some
larger that the picture box it will be added to.

How do I get the form to resize when I add a User Control to the
picture box control when the User Control is larger than the picture
box?

Why are you using a PictureBox? That's wholly inappropriate as a
container control.

If you were using a proper container control, you'd have the AutoSize and
AutoSizeMode properties that would allow you to do this automatically.
Just set AutoSize to true and AutoSizeMode to AutoSizeMode.GrowAndShrink.
Then as items are added or removed from the control, it will resize
appropriately.

Pete
 
Z

zacks

If you're not talking about fitting picturebox on a form which that's
higher on screen / desktop resolution (which i'd like to accomplish),
and you might have mentioned about a kind of alignment / object
fitting issue, i think you should do:

Picurebox1.size = <User_control>.size ' Assuming user control
providing a GUI and size info.
' Apply the same for the form

Me.size = Picturebox1.size ' Or add some space "Picturebox1.size + 50"

Hope this helps.


Not "automatic" but it has the distinct advantage of working.

picturebox.Controls.Clear();
this.Height = this.Height + (usercontrol.Height - picturebox.Height);
this.Width = this.Width + (usercontrol.Width - picturebox.Width);
picturebox.Height = usercontrol.Height;
picturebox.Width = usercontrol.Width;
picturebox.Controls.Add(usercontrol);
this.CenterToScreen();
 
K

kimiraikkonen

Not "automatic" but it has the distinct advantage of working.

picturebox.Controls.Clear();
this.Height = this.Height + (usercontrol.Height - picturebox.Height);
this.Width = this.Width + (usercontrol.Width - picturebox.Width);
picturebox.Height = usercontrol.Height;
picturebox.Width = usercontrol.Width;
picturebox.Controls.Add(usercontrol);
this.CenterToScreen();- Hide quoted text -

- Show quoted text -

So did it work for you?
 
P

Peter Duniho

Not "automatic" but it has the distinct advantage of working.

picturebox.Controls.Clear();
this.Height = this.Height + (usercontrol.Height - picturebox.Height);
this.Width = this.Width + (usercontrol.Width - picturebox.Width);
picturebox.Height = usercontrol.Height;
picturebox.Width = usercontrol.Width;
picturebox.Controls.Add(usercontrol);
this.CenterToScreen();

But again, why are you using a PictureBox? Why not use a more appropriate
control that already implements this automatic behavior?

Even if you want a PictureBox in there somewhere, for the purpose of
displaying an image, there's no need to make the PictureBox the container
itself. Use a real container, and put into it both the PictureBox and
your UserControl. You can even have the PictureBox anchored so that it
resizes as necessary as the container resizes according to the size of the
UserControl. And depending on how you want the image displayed, you could
simply set the background image for the container control and not bother
with a PictureBox at all.

You seem to be implementing very awkward code for no good reason. At the
very least, if there's really a good reason for using a PictureBox, it
would help to understand your question better if you could explain what
that reason is.

Pete
 
J

Joe Cool

Why are you using a PictureBox? That's wholly inappropriate as a
container control.

If you were using a proper container control, you'd have the AutoSize and
AutoSizeMode properties that would allow you to do this automatically.
Just set AutoSize to true and AutoSizeMode to AutoSizeMode.GrowAndShrink.
Then as items are added or removed from the control, it will resize
appropriately.

What control would be a "proper container control"? Panel?
 
P

Peter Duniho

What control would be a "proper container control"? Panel?

Sure. Or UserControl. Or a GroupBox. Or any other control that was
specific designed to act as a container for other controls All controls
support having child controls, but most are not actually intended to be
containers for other controls. The PictureBox control is definitely not a
container control.

The use of child controls in non-container controls would be more for
compositing operations (though the lines are blurry even there, as the
UserControl is advertised as much as a way to create a single new control
that's a composite of other controls as it is for acting as a container
for a group of otherwise unrelated controls). Since all controls can have
children controls, the fact that you can add children to a control doesn't
in any way imply it's suitable for acting as a container.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top