简体   繁体   English

对齐文本框和标签文本

[英]Align TextBox and Label text

I'm designing a UI, and I found myself itching my head : how can I align a TextBox text and a label text, which are side by side. 我在设计UI时,发现自己很头疼:如何对齐文本框文本和标签文本。

In design mode, it's easy, you move one with your mouse, a purple line appears and voila ! 在设计模式下,这很容易,只要用鼠标移动一个,就会出现一条紫色的线,瞧! the alignment is good, but mine are code generated, so how can i align their contents ? 对齐方式很好,但是我的代码是生成的,那么如何对齐它们的内容?

Thank you ! 谢谢 !

Edit : Layout is something I can't use (I don't make the rules, my boss do..) 编辑 :布局是我不能使用的东西(我不制定规则,老板做..)

I like to use the FlowLayoutPanel (instead of the TableLayoutPanel ) for this purpose because you don't need to fiddle with columns. 我喜欢为此目的使用FlowLayoutPanel (而不是TableLayoutPanel ),因为您不需要摆弄列。 Remember to remove both the Top and the Bottom anchors on every control to make them vertically centered, and set FlowLayoutControl.AutoSize = true and AutoSizeMode = GrowAndShrink. 记住要同时删除每个控件上的顶部和底部锚,以使其垂直居中,并设置FlowLayoutControl.AutoSize = true和AutoSizeMode = GrowAndShrink。

Edit : regarding your restriction that "Layout is something I can't use": so you want instead to access the purple text baseline snapline position programmatically, at runtime? 编辑 :关于“布局是我不能使用的东西”的限制:因此,您想在运行时以编程方式访问紫色文本基线对齐线位置? This is possible, but it's unlikely to be faster than layouts because only the designer for the control knows where it is, so you will have to create designers for all controls you need this for. 这是可能的,但是它不可能比布局更快,因为只有控件的设计者才知道它在哪里,因此您将必须为需要此控件的所有控件创建设计器。

This question has some code that can be used as a starting point, but as I said, it's probably not the right approach either given the performance constraints. 这个问题有一些可以用作起点的代码,但是正如我所说,鉴于性能限制,这可能也不是正确的方法。

Take a look at the TableLayoutPanel. 看一下TableLayoutPanel。 Still not so easy to get the baseline match but by vertically centering the label and setting the Rows to AutoSize you will get something that is ordered and flexible. 仍然很难获得基线匹配,但是通过垂直居中放置标签并将“行”设置为“自动调整大小”,您将获得有序且灵活的内容。

then use the X, Y, Width, Height properties of each control (inherited from Control). 然后使用每个控件的X,Y,Width,Height属性(从Control继承)。

int padding = 5;
textbox.Y = label.Y;
textbox.X = label.Width + padding

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

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