Click here to Skip to main content
Click here to Skip to main content

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 -->

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

Share

About the Author

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

Comments and Discussions

 
Generalo dear Pinmemberleppie24-Jun-04 8:55 
GeneralRe: o dear PinmemberACanadian24-Jun-04 9:17 
GeneralRe: o dear Pinmemberlshzhou5-Jul-04 16:56 
GeneralRe: o dear Pinmemberleppie5-Jul-04 17:40 
GeneralRe: o dear Pinmemberlshzhou5-Jul-04 16:59 
GeneralA couple of thoughts PinmemberMichael Potter24-Jun-04 8:39 
GeneralRe: A couple of thoughts PinmemberACanadian24-Jun-04 9:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 24 Jun 2004
Article Copyright 2004 by ACanadian
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid