Results 1 to 3 of 3

Thread: Text Width

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Text Width

    Howdy all,

    I was wondering if there is a way to determine the width of text in a RichTextBox (in pixels or otherwise). The RichTextBox will have different sized fonts aswell.

    RichTextBox1


    Do I use GetTextMetrics? If so how? If not, what should I use?

    Thanks in advance

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    Well, you could parse the TextRTF property for different fonts and use TextWidth of a hidden picturebox (I'd use that instead of the form because you'd have to change the remaining width so you don't go over the edge of your box), add them together, and get your answer. Hmmmm, there must be a simpler way.... maybe some weird SendMessage on the RTB?
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    I tried doing something like you suggested, like this:

    VB Code:
    1. For i = 0 To Len(rtb.SelText)
    2.     rtb.SelStart = i
    3.     rtb.SelLength = 1
    4.     With rtb
    5.         Form1.pic.FontName = .SelFontName
    6.         Form1.pic.FontSize = .SelFontSize
    7.         Form1.pic.FontBold = .SelBold
    8.         Form1.pic.FontItalic = .SelItalic
    9.         Form1.pic.FontStrikethru = .SelStrikeThru
    10.         Form1.pic.FontUnderline = .SelUnderline
    11.         Form1.pic.Print .SelText
    12.     End With
    13. Next i
    14.  
    15. widthIWant = Form1.pic.TextWidth(rtb.SelText)

    It seems to work, but there is some math involved (since it isn't right every time). A Simpler way would be nice..

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width