views:

733

answers:

2

Hello !

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.

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..)

+1  A: 

then use the X, Y, Width, Height properties of each control (inherited from Control).

int padding = 5;
textbox.Y = label.Y;
textbox.X = label.Width + padding
Matt Jacobsen
I already found this solution, but we're using AppStyling, which define text size etc. If the design change, I would have to change the padding.
Clement Herreman
Taht aligns the Tops of the two controls which is ugly
Henk Holterman
what the hell is AppStyling?
Matt Jacobsen
@Henk Holterman: With no layout - this isn't ever gonna be pretty.
Matt Jacobsen
@Matt We're using Infragistics Winform controls, which you can specify a style that modify Appareance property of your graphical controls when your application begin to run.
Clement Herreman
+1  A: 

I like to use the FlowLayoutPanel (instead of the TableLayoutPanel) for this purpose because you don't need to fiddle with columns. 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.

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.

romkyns
2 Q's: 1. Doesn't flow layout simply vertically center the controls? This does not necessarily mean that the text baselines are aligned (at least not in general, it might suffice for text box and label). 2. If the flow layout *does* align the text baselines, and the designer is the only one who knows the baseline, how could the flow layout provide a faster implementation that one using the designer?
chiccodoro
@chiccodoro: you're right, the baselines won't necessarily be aligned, and I agree that this sucks. It just sucks less than any other options I am aware of.
romkyns
@romkyns: In what in turn you're right :-) Actually vertically centering the controls is now exactly what I did in my case (had a similar problem, therefore I found this thread), and it suffices
chiccodoro