views:

900

answers:

1

I'm working on a project updating their WinForms application UI to be more consistent with sizes. TextBox and ComboBox controls have different heights by default, even with the same font. I've been able to resize the text boxes by turning off AutoSize, but the text still hugs the top of the control, leaving a gap below.

Is there any way to center the text vertically in the control?

+1  A: 

If you're turning off AutoSize on a control, it must be a Label, since TextBox doesn't have an AutoSize property. The TextAlign property of a Label is of type ContentAligment, so you can set both horizontal and vertical alignment.

For various boring reasons, TextBoxes in windows are intended to auto-adjust their heights to the font used. To control the heights and vertically center the text, you can quickly create a custom UserControl that you can replace all your textboxes with.

On your user control, set the BorderStyle to Fixed3D and the BackColor to System.Window. Add a TextBox and set its BorderStyle to None. In the Resize event for the control, add code that makes the TextBox the same width as the user control's client area (accounting for the border pixels) and left-aligns it (i.e. textBox1.Left = 0;) and vertically centers it (e.g. textBox1.Top = (this.Height - textBox1.Height) / 2;).

Finally, add to the user control any TextBox-type properties and events you need (probably just Text and TextChanged, I would guess), and wire them up so that they pass through to the TextBox inside your control, like this:

public string Text
{
    get
    {
        return textBox1.Text;
    }
    set
    {
        textBox1.Text = value;
    }
}

If you wanted to get super-fancy with this, you could even replace your user control's TextAlign property with one that is actually of type ContentAlignment (like the Label) and then align the inner TextBox to match.

This same approach will work for a ComboBox, although it will look slightly odd. With the ComboBox, you set its FlatStyle property to Flat - otherwise you deal with it the same as a TextBox. It will look odd because the dropdown arrow box won't be quite at the top and bottom of the panel.

MusiGenesis
Just as an FYI, there is in fact an AutoSize property on TextBoxes. It just won't show up in the property sheet or auto-complete due to some Attribute decorations. But myTextBox.AutoSize = false; will compile, and putting that in the constructor of an inherited control will result in a resizable text box.
Clyde
Noted. It's been years since I stopped trying to make Windows controls look different than the standard, so I'm a bit rusty. It's interesting that the Intellisense says "This property is not relevant for this class", even though it obviously *is* (setting it to false does indeed make the height adjustable).
MusiGenesis
I guess the easier answer is: switch to WPF. Everything has a VerticalContentAlignment and a HorizontalContentAlignment property.
MusiGenesis
oh how I'd love to switch this to wpf.........
Clyde
I think that's precisely why they hid the property -- adjusting height is pointless if you can't adjust the alignment.I'm trying out your solution now to see if it'll work for us.
Clyde
I'm pretty sure there's a Win32-API, message-hooking way of vertically aligning a text box, but I can't find it. I think the user control way would be less work, ultimately, and you'd have greater control over the final output.
MusiGenesis