Click here to Skip to main content
15,886,778 members
Articles / Programming Languages / C#
Article

Height of Text

Rate me:
Please Sign up or sign in to vote.
1.63/5 (6 votes)
23 Jun 2004 39.9K   7   7
This function will return the height of text. Designed for use with text boxes on windows forms.

Introduction

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

Overview

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.

License

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


Written By
Chief Technology Officer Wield Software Development Inc
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalo dear Pin
leppie24-Jun-04 7:55
leppie24-Jun-04 7:55 
GeneralRe: o dear Pin
ACanadian24-Jun-04 8:17
ACanadian24-Jun-04 8:17 
GeneralRe: o dear Pin
lshzhou5-Jul-04 15:56
lshzhou5-Jul-04 15:56 
GeneralRe: o dear Pin
leppie5-Jul-04 16:40
leppie5-Jul-04 16:40 
GeneralRe: o dear Pin
lshzhou5-Jul-04 15:59
lshzhou5-Jul-04 15:59 
GeneralA couple of thoughts Pin
Michael Potter24-Jun-04 7:39
Michael Potter24-Jun-04 7:39 
GeneralRe: A couple of thoughts Pin
ACanadian24-Jun-04 8:16
ACanadian24-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.