Introduction
This article describes how to set the height of a single-line textbox
control. The TextBox
control in C# (and VB.NET) ignores the height property and adjusts it to fit the font height. By calculating the exact font size needed, we can set the TextBox
to a desired height.
Background
I had a C# project that required a single-line textbox
with adjustable height. I found many examples of how to adjust the width, but nothing on how to dynamically change the height of a textbox
. I did not want to use multi-line because I wanted to keep the auto-complete features of the single-line textbox
.
Using the Code
Single-line textbox
height is set by the size of the font, not the TextBox.Height
property. This makes it difficult if you are looking for an exact height. Luckily, the font property uses a float for the font size (emSize
). You can use fractions of fonts to fine-tune the textbox
height.
The calculation the textbox
uses to determine its height is:
Height = ( Font Size * Font Line Spacing / Font Em Height ) + 7
- Font Size - It is easiest to measure font size in pixels, so you do not have to factor in screen dpi.
- Font Line Spacing - The distance, in design units, between two consecutive lines of text.
- Font Em Height - Height, in design units of the font's widest letter - typically the letter M.
Textbox
es have a 3-pixel lower and 4-pixel upper white space around the font height. Therefore, the calculation increases the height by 7 pixels.
We can reverse this calculation to obtain the font size needed for a desired height:
Font Size = ( height - 7 ) * Font Em Height / Font Line Spacing
This method will return a font
object that will set the size of your textbox
:
private Font GetFontForTextBoxHeight(int TextBoxHeight, Font OriginalFont)
{
float desiredheight = (float)TextBoxHeight;
Font fnt = new Font(OriginalFont.FontFamily,
OriginalFont.Size,
OriginalFont.Style,
GraphicsUnit.Pixel);
if (desiredheight < 8)
desiredheight = 8;
float FontEmSize = fnt.FontFamily.GetEmHeight(fnt.Style);
float FontLineSpacing = fnt.FontFamily.GetLineSpacing(fnt.Style);
float emSize = (desiredheight - 7) * FontEmSize / FontLineSpacing;
fnt = new Font(fnt.FontFamily, emSize, fnt.Style, GraphicsUnit.Pixel);
return fnt;
}
Whenever you have to set a textbox
size, set the font
property using the above method:
YourTextBox.Font = GetFontForTextBoxHeight(DesiredHeight, YourTextBox.Font);
Using the Control
The adjustable Height Textbox
is a user control that you can add to your form. It behaves the same as a regular TextBox
except:
- You can set the height in the design view via the
Size_AdjustableHeight
property.
- The control, if in single-line mode, will respond to docking and anchoring.
To use the control:
- Right-click on your toolbox in Design view.
- Click Browse. Navigate to AdjustableHeightTextbox.dll. Hit OK.
- Add the control to your form.
Enjoy!
Points of Interest
- I learned that the .NET
TextBox
code is basically a wrapper for the old MFC textbox
control. That is why it is not very customizable.
- The
GetFontForTextBoxHeight()
routine, with some tweaking, could be used for a ComboBox
control. The height calculations are close, but still off by a couple of pixels.
There is a lack of information on the inner workings of the textbox
control (as with most of the standard .NET controls). I hope this article has saved you hours of searching.
History
- 2008.09.07 - Posted original article
- 2008.09.10 - Added a user control version of the
textbox
and a test project