Vertically aligning controls in a TableLayoutPanel
Is there any way to have textual content of controls on a TableLayoutPanel align themselves properly? I've got labels in column 0, and textboxes (or occasionally ComboBox or NumericUpDown controls开发者_开发问答) in column 1, but the text in the label is usually a pixel or two out (vertically) on most rows.
Select all the controls in your table layout panel. In the properties tab ensure anchor set to 'left'. I think it defaults to 'top,left'.
Hope that sets things straight.
I worked on it a few days back. I got everything as required. Try this:
For Labels
- Detach label from Tablelayoutpanel
- Set dockstyle to top
- Set anchor points none
TextAlign = MiddleLeft
- Then just place it in the table layout column
- Make sure the row style height matches label height if you have a label backcolor.
For Textboxes
- Detach textboxes from Tablelayoutpanel
- Set dockstyle to none
- Set anchor points none
TextAlign = MiddleLeft
(or as required)- Then just place it in the table layout column
You can set following properties:
for Label
1.Autosize= false;
2.TextAlign= MiddleLeft;
for Controls like Combobox,Textbox
goto View -> Toolbars -> Layout
Now you can give layout of your controls very easily by the Toolbar by selecting Multiple Controls at a Time............
I'd recommend leaving the anchors as they are and just adding a top margin to the controls that are too high. A margin of 3,6,3,0 is normal for me when I'm adding labels. As Javed said you can select multiple controls by control clicking. It irritates me that ComboBoxes are one pixel larger than Texboxes. Can't do much about that though.
Oh, you mean that text baseline for label is different than for other controls? If so, try to remove vertical anchors. You may play with margins as well.
What I usually do is put them in a container eg a panel and anchor them relatively to that. The advantages of adding a container is you can also reduce the no of Win handlers. In that case that wouldn't matter , because you already have the TablelayoutPanel so basically for all the controls you only have a single handler.
精彩评论