5,699,997 members and growing! (12,345 online)
Email Password   helpLost your password?
Languages » C# » How To License: The Code Project Open License (CPOL)

Set TextBox Height

By hayes.adrian

How to change the height of a single-line textbox
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows (Windows, Win2K, WinXP, Win2003, Vista, TabletPC), .NET (.NET, .NET 2.0)

Posted: 7 Sep 2008
Updated: 7 Sep 2008
Views: 4,995
Bookmarked: 12 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.16 Rating: 3.09 out of 5
2 votes, 40.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4
1 vote, 20.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 text box with adjustable height.  I found many examples of how to adjust the width, but nothing on how to dynamically change the height of a text box.  I did not want to use multi-line because I wanted to keep the auto-complete features of the single-line text box.

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.
Text boxes 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 text box:
private Font GetFontForTextBoxHeight(int TextBoxHeight, Font OriginalFont)
{
    // What is the target size of the text box?
    float desiredheight = (float)TextBoxHeight;

    // Set the font from the existing TextBox font.
    // We use the fnt = new Font(...) method so we can ensure that
    //  we're setting the GraphicsUnit to Pixels.  This avoids all
    //  the DPI conversions between point & pixel.
    Font fnt = new Font(OriginalFont.FontFamily,
                        OriginalFont.Size,
                        OriginalFont.Style,
                        GraphicsUnit.Pixel);

    // TextBoxes never size below 8 pixels. This consists of the
    // 4 pixels above & 3 below of whitespace, and 1 pixel line of
    // greeked text.
    if (desiredheight < 8)
        desiredheight = 8;

    // Determine the Em sizes of the font and font line spacing
    // These values are constant for each font at the given font style.
    // and screen DPI.
    float FontEmSize = fnt.FontFamily.GetEmHeight(fnt.Style);
    float FontLineSpacing = fnt.FontFamily.GetLineSpacing(fnt.Style);

    // emSize is the target font size.  TextBoxes have a total of
    // 7 pixels above and below the FontHeight of the font.
    float emSize = (desiredheight - 7) * FontEmSize / FontLineSpacing;
    
    // Create the font, with the proper size.
    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: 

  1. Right-click on your toolbox in Design view
  2. Click Browse.  Navigate to AdjustableHeightTextbox.dll.  Hit OK.
  3. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

hayes.adrian


I am a Senior System Administrator for a 400+ server ASP farm. With such a large farm and limited staff, our goal is to add as much automation as possible to the system. Most of my programming consists of intelligent slack: spending 2 hours to write a program that handles a reoccurring 10 minute manual job.
Occupation: Systems Engineer
Company: ThomsonReuters Tax & Accounting
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralTextBox.AutoSize missing?memberhayes.adrian8:00 10 Sep '08  
GeneralAh!memberIlíon7:02 17 Sep '08  
GeneralRe: TextBox.AutoSize missing? [modified]memberIlíon9:23 17 Sep '08  
QuestionIs it official?membersk8er_boy28722:34 8 Sep '08  
AnswerRe: Is it official?memberIlíon0:49 9 Sep '08  
GeneralRe: Is it official?membersk8er_boy2870:55 9 Sep '08  
GeneralRe: Is it official?memberhayes.adrian5:19 9 Sep '08  
GeneralRe: Is it official?membersk8er_boy28723:26 9 Sep '08  
GeneralRe: Is it official? [modified]memberhayes.adrian6:53 10 Sep '08  
GeneralRe: Is it official?memberIlíon0:44 10 Sep '08  
JokeRe: Is it official?membersk8er_boy2871:06 10 Sep '08  
AnswerRe: Is it official? [modified]memberhayes.adrian4:54 9 Sep '08  
GeneralRe: Is it official?membersk8er_boy28723:29 9 Sep '08  
GeneralWhat about a nested control?membersupercat910:03 8 Sep '08  
GeneralThat's useful to know. And a suggestion. [modified]memberIlíon4:43 8 Sep '08  
GeneralRe: That's useful to know. And a suggestion.memberhayes.adrian14:04 8 Sep '08  
GeneralRe: That's useful to know. And a suggestion.memberhayes.adrian15:04 10 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Sep 2008
Editor:
Copyright 2008 by hayes.adrian
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project