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?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With