Click here to Skip to main content
Licence 
First Posted 23 Jun 2004
Views 26,514
Bookmarked 7 times

Height of Text

By | 23 Jun 2004 | Article
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

About the Author

ACanadian



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalo dear Pinmemberleppie7:55 24 Jun '04  
GeneralRe: o dear PinmemberACanadian8:17 24 Jun '04  
GeneralRe: o dear Pinmemberlshzhou15:56 5 Jul '04  
GeneralRe: o dear Pinmemberleppie16:40 5 Jul '04  
GeneralRe: o dear Pinmemberlshzhou15:59 5 Jul '04  
GeneralA couple of thoughts PinmemberMichael Potter7:39 24 Jun '04  
GeneralRe: A couple of thoughts PinmemberACanadian8:16 24 Jun '04  

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.

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