Click here to Skip to main content
13,200,729 members (53,701 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 23 Jun 2004

Height of Text

, 23 Jun 2004
Rate this:
Please Sign up or sign in to vote.
This function will return the height of text. Designed for use with text boxes on windows forms.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Add the rest of your HTML here -->


This is a handy little function for getting the height of text. 


I have read many articles and asked lots of questions before I found the functions in the dot net library that would help me to achieve this task.

This is also my very first Code Project article. This site has helped me solve MANY problems and I hope that this piece of code helps someone out.

The Code

<BR>  /// <summary><BR>  /// This method will return the height of the string passed in.<BR>  /// </summary><BR>  /// <param name="strMeasureString">The string to measure</param><BR>  /// <param name="stringFont">The font to measure by</param><BR>  /// <param name="nWidth">The max width the string can be [Width of the text box usually ;)]</param><BR>  /// <param name="hwnd">Handle to your form so I can get the graphics object</param>  <BR>  /// <returns>The height of the string as a int</returns> <FONT color=#0000ff size=2><P> </P><P>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> GetStringHeight(</FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> strMeasureString, System.Drawing.Font stringFont, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> nWidth,System.IntPtr hwnd) </P><P>{</P><P></FONT><FONT color=#0000ff size=2>	using</FONT><FONT size=2> (System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(hwnd))</P><P>	{</P><P></FONT><FONT color=#0000ff size=2>		return</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)g.MeasureString(strMeasureString, stringFont, nWidth).Height; </P><P>	}</P><P>}</P></FONT>

(Thanks for the advice Micheal)

Its pretty straightforward and it creates some pretty cool effects if you use it with the 'Text Changed' event of a text box. Tie that to the 'Resize' event and you can have a form the grows and shrinks as the user is typing in text.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chief Technology Officer Wield Software Development Inc
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

Generalo dear Pin
leppie24-Jun-04 7:55
memberleppie24-Jun-04 7:55 
GeneralRe: o dear Pin
ACanadian24-Jun-04 8:17
memberACanadian24-Jun-04 8:17 
GeneralRe: o dear Pin
lshzhou5-Jul-04 15:56
memberlshzhou5-Jul-04 15:56 
GeneralRe: o dear Pin
leppie5-Jul-04 16:40
memberleppie5-Jul-04 16:40 
GeneralRe: o dear Pin
lshzhou5-Jul-04 15:59
memberlshzhou5-Jul-04 15:59 
GeneralA couple of thoughts Pin
Michael Potter24-Jun-04 7:39
memberMichael Potter24-Jun-04 7:39 
GeneralRe: A couple of thoughts Pin
ACanadian24-Jun-04 8:16
memberACanadian24-Jun-04 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 24 Jun 2004
Article Copyright 2004 by ACanadian
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid