Managed Extensions: Measuring Strings

Welcome to this week's installment of .NET Tips & Techniques! Each week, award-winning Architect and Lead Programmer Tom Archer demonstrates how to perform a practical .NET programming task using either C# or Managed C++ Extensions.

A recent client of mine wanted text to be displayed in a Label (static) control only if the text could fit. If the text could not fit into the Label, the user was to see a string indicating that the text was too long and they could double-click the item to drill down into the details of the value (which displayed another dialog). Using .NET, this turned out to be much easier than I first anticipated.

I've created a simple Managed Extensions demo to illustrate how to do this. Here's a figure of that application being run a few times:

As you can see, the code is tested by entering text into a multi-line TextBox (edit) control and then clicking the Display button. If the text fits nicely into the Label control, it's displayed. If not, the text "Too large" is displayed instead.

This is accomplished by performing the following steps:

  1. Call the Label object's CreateGraphics method. This method returns a Graphics object for the current device context.
  2. Call the Graphics object's MeasureString method, passing it the string to be measured and the font that will be used to display the string. The return value will be a System::Drawing::SizeF object that contains the height and width of the string as it will be rendered in the current device context.
  3. Compare the SizeF, Height, and Width property values to the Height and Width properties of the Label control to determine whether the text will fit.

Here's how that code would look (copied from the article's demo):

private: System::Void button1_Click(System::Object *  sender,
                                    System::EventArgs *  e)
{
   Graphics* g = label1->CreateGraphics();
   SizeF size = g->MeasureString(textBox1->Text,label1->Font);
   if (size.Width < label1->Width
   && size.Height < label1->Height)    // fits
   {
      label1->Text = textBox1->Text;
   }
   else    // doesn't fit
   {
     label1->Text = S"Too large";
   }
}

The last two things to point out are:

  • I pass the TextBox object's text and the Label object's font.
  • I compare on both height and width because a carriage-return/line-feed pair (the TextBox MultiLine property is set to True) would not be measured because they're not displayable characters. Therefore, I want to make sure that the text also displayed vertically.


About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds