none
How to measure the Text string in a ComboBox? RRS feed

  • Question

  • I'm wondering how (or if it's possible) to measure the length (in pixels) of the string contained within a ComboBox Text property.  I can measure the string in a regular TextBox by the following code:

     

    Rect textRect = tb.GetRectFromCharacterIndex(nLength);

    SetValue(TextSizePropertyKey, textRect.Right);

     

    However, there is no comparable GetRectFromCharacterIndex() method available for the ComboBox.  Is there another way to do this?

     

     

    Friday, January 18, 2008 5:40 PM

Answers

  • Code Block

    FormattedText sFT = new FormattedText(this.Text,

    System.Globalization.CultureInfo.GetCultureInfo("en-us"),

    FlowDirection.LeftToRight,

    new Typeface(this.FontFamily.ToString()),

    this.FontSize,

    Brushes.Black);

    // Find the width of the string in pixels

    double nWidth = sFT.Width;

    SetValue(TextSizePropertyKey, nWidth);

     

     

    Friday, January 18, 2008 5:52 PM