C# WinForms Vertical Alignment for TextBox, etc
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. 开发者_如何学Python
Is there any way to center the text vertically in the control?
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 height and vertically center the text, you can quickly create a custom UserControl
, that you can use for replacing all your TextBoxes
with.
On your UserControl
, 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 => 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 works 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 drop-down arrow box won't be quite at the top and bottom of the panel.
Create an empty Control
and include your TextBox
as a child. Then when the parent Control
or the TextBox
, resize realign your TextBox
control in the middle vertically.
Remove the borders, make the background the same color as the parent (default). Override the font to set the TextBox
font and I think you have will have a vertically aligned TextBox
.
Did you try the TableLayoutPanel
solution to provide vertical alignment? You can then adjust the height as you wish, dynamically through events, OR by using a fixed value, OR with the TableLayoutPanel
's row AutoSize feature.
Just add a TableLayoutPanel
to contain the TextBox
. This TableLayoutContainer
has 1 column and 3 rows. The TextBox
must be placed in the second row. The first and the last rows are set to 50% height (they actually use only the "remaining height"). After you placed the TextBox
in the second row, this row can then be set to an absolute value of your choice or to Auto-size.
The TableLayoutPanel
has its own control of width and height. But if you are placing this TableLayoutPanel
inside a cell of another TableLayoutPanel
, just remember to set the property Auto-size of your new TableLayoutPanel
to true, so it can adjust itself to the size of the cell.
In general, TableLayoutPanels
are of great help for your design, I definitely recommend using them.
A very simple solution is to use a Datagridview
with 1 column, 1 row, column and row headers not visible and do DefaultCellStyle.Alignment = MiddleLeft
.
And disable adding/deleting of rows and you have a textbox that perfectly aligns the text.
You can simply adjust padding - vertical higher. Property in category layout.
精彩评论