How to determine minimum size of a container to show all controls in the container?

J

John

Hi,

I have GroupBoxes and Panels of varying sizes that I dynamically
display on my Windows Forms. Is there a way that I can calculate the
minimum size of a GroupBox or a Panel to display all the controls that
it contains without clipping the edges?

I have a GroupBox that I add rows of TextBoxes to. I don't know how
many TextBoxes I'll need to add at compile time. So at run-time, I'd
like to call some method to tell me the minimum size required to
display all the textboxes in the GroupBox and then resize the GroupBox
to show all the TextBoxes.

Thanks,
John
 
T

Tommy Carlier

John said:
Hi,

I have GroupBoxes and Panels of varying sizes that I dynamically
display on my Windows Forms. Is there a way that I can calculate the
minimum size of a GroupBox or a Panel to display all the controls that
it contains without clipping the edges?

I have a GroupBox that I add rows of TextBoxes to. I don't know how
many TextBoxes I'll need to add at compile time. So at run-time, I'd
like to call some method to tell me the minimum size required to
display all the textboxes in the GroupBox and then resize the GroupBox
to show all the TextBoxes.

Thanks,
John

Here you can find some (untested) code. Keep in mind that you should
probably add a few pixels to both minWidth and minHeight after the loop,
for the borders of the groupbox.

int minWidth = 0, minHeight = 0;
foreach(Control ctl in group.Controls)
{
int width = ctl.Left + ctl.Width;
int height = ctl.Top + ctl.Height;
if (width > minWidth) minWidth = width;
if (height > minHeight) minHeight = height;
}
 

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