Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

resizing controls by percent

Status
Not open for further replies.

gib99

Programmer
Mar 23, 2012
51
CA
Hello,

I have a UserControl with a line up of textboxes and comboboxes on it. They are lined up from left to right with the texboxes coming first and then the comboboxes

What I would like to do is allow for the UserControl to be horizontally resized and have the textboxes and comboboxes resize according to percentage. Whatever percentage they are now, they should maintain that percentage. So, for example, the left-most combobox is about 8% of the entire width of the UserControl. If the UserControl was stretched to twice its current size, the combobox should also stretch to twice its size, thereby remaining at 8% of the UserControl's width.

How can I do this?

I'm working in Visual Studios 2008.
 
I would use a TableLayoutPanel with multiple columns and one row, and place your controls within that. You can size the TableLayoutPanel columns using percents, and if the TableLayoutPanel is docked within your user control those columns should size (by percent) on the size of the UserControl. Just be sure your control Anchors are set properly (Anchor Left & Right) to automatically fill the columns width.
 
Thanks very much, Borvik,

Mind if I ask one more question?

Using the TableLayoutPanel, how can I tell it to remove any space or padding between the controls so that the cell "hug" the controls and no gaps exist between controls?

Thanks again.
 
That would be the Textbox and ComboBox Margin properties causing that. The default margins are 3,3,3,3. If you set the left and right margins to 0, that should eliminate the gaps.
 
Thanks again Borvik,

I hope you don't mind if I ask you a few more questions.

I'm having trouble controlling the initial sizes of the cells and their contents. The visual studios designer seems to be doing some pretty erradic things with the sizes and I can't really control it. One control (a textbox) had an original width of 250 pixels, but then when I place it into a cell, it reduces to 60. I can't manually adjust the width back to 250 in the properties because it reverts it back to 60. The cell borders won't move when I dry to drag them.

Programmatically, I'm not sure how to access each cell individually and set their widths in terms of percent.

I need either: 1) to set the control to a particular width and have the table cells conform to those widths, or 2) set the table cells to a certain width (in terms of percentage) and have the control conform to those widths.

Any help would be very much appreciated. Thanks.
 
You can use the Visual Designer of the UserControl itself.

With the TableLayoutPanel selected in the Properties window (the status area of it) click on Edit Rows and Columns. This window will allow you to control the column sizes: absolute widths, autosize to contents, or percentages.

As for the default width. In the Visual Designer of the UserControl, with the UserControl itself selected, change the default width of this until the controls are the proper width in their columns. This should be the default width of the UserControl when you add it to your forms, and the sizes should be more what you are expecting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top