Reputation: 936
I am developing a quick app in C#. One thing that I want to do is the equivalent of Swing's (java) pack()
. This is a call that allows me to say to a form (Frame in Java), resize yourself to the size of the sum of the components within yourself.
I have searched and searched (the components change dynamically so I cannot simply hardcode the form to the right size) but I cannot find the equivalent method in C#.
Does anyone know what it is called?
Upvotes: 10
Views: 6059
Reputation: 1270
Another solution, available only if the image is smaller than the screen:
Form frm = new Form();
PictureBox pbx = new PictureBox();
pbx.Image = Image.FromFile( _imagePath.Text );
pbx.Dock = DockStyle.Fill;
frm.Controls.Add( pbx );
frm.Size = pbx.Image.Size;
frm.Show();
Upvotes: 0
Reputation: 16121
You don't even need write code in order to 'pack' the form. In the forms designer, set the form's AutoSize
property to true
, and set the AutoSizeMode
property to GrowAndShrink
, repeat this for any controls on the form which may also change size.
And voila!
At runtime (only), the form will dynamically resize itself to accommodate all the controls. If you want it to keep a little distance from the controls so that the controls won't 'stick' to the edges, you can set the Padding
property (I'd recommend at least a value of 12,12,12,12).
Upvotes: 11