简体   繁体   English

垂直对齐TableLayoutPanel中的控件

[英]Vertically aligning controls in a TableLayoutPanel

Is there any way to have textual content of controls on a TableLayoutPanel align themselves properly? 有没有办法让TableLayoutPanel上的控件的文本内容正确对齐? 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. 我在第0列中有标签,在第1列中有文本框(或偶尔使用ComboBox或NumericUpDown控件),但标签中的文本通常是大多数行上的一个像素或两个(垂直)。

I worked on it a few days back. 几天前我就开始工作了。 I got everything as required. 我按要求得到了一切。 Try this: 尝试这个:

For Labels 对于标签

  • Detach label from Tablelayoutpanel 从Tablelayoutpanel分离标签
  • Set dockstyle to top 将dockstyle设置为顶部
  • 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 从Tablelayoutpanel分离文本框
  • Set dockstyle to none 将dockstyle设置为none
  • Set anchor points none 设置锚点无
  • TextAlign = MiddleLeft (or as required) TextAlign = MiddleLeft (或根据需要)
  • Then just place it in the table layout column 然后将其放在表格布局列中

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. 希望能把事情做好。

You can set following properties: 您可以设置以下属性:

for Label 用于标签
1. Autosize= false; 1. Autosize= false;
2. TextAlign= MiddleLeft; 2. TextAlign= MiddleLeft;

for Controls like Combobox,Textbox 对于像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. 当我添加标签时,我可以获得3,6,3,0的保证金。 As Javed said you can select multiple controls by control clicking. 正如Javed所说,您可以通过控制点击选择多个控件。 It irritates me that ComboBoxes are one pixel larger than Texboxes. 令人恼火的是,ComboBoxes比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. 添加容器的优点是您还可以减少Win处理程序的数量。 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. 在那种情况下无关紧要,因为你已经拥有TablelayoutPanel所以基本上所有控件都只有一个处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM