Results 1 to 5 of 5

Thread: Line height RichTextBox

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Line height RichTextBox

    I was wondering how I would go about getting the height of a line in a RichTextBox from a character index.
    When I say line I don't mean split by line breaks but lines as they are displayed on the screen (with word breaks).
    I need it to take into consideration the actual height that is taken up by that line ... which won't necessarily be the height of the font as there may be a larger font on that line later that makes the line larger.

    How can I go about this?

    On a side note I have always thought that the implementation of the RichTextBox was a little dodgy... so i preferably don't want a dodgy way to do this.

    Thanks in advance,
    Kris

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Line height RichTextBox

    I'm not aware of anything built into the RichTextBox control to do that. All I can think of is checking all the Fonts used on the line containing that character and taking the largest one.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Line height RichTextBox

    The event ContentsResized is raised when the text of a RichTextBox is, well resized. So if you could copy the line with all the formatting you might be able to put it inside another (hidden or in memory) RichTextBox and use this event to get the new rectangele size.
    Code:
    Private Sub HiddenRTB_ContentsResized(sender As Object, e As ContentsResizedEventArgs)
      _lineHeight = e.NewRectangle.Height
    End Sub
    I know, it's a bit of a hack but it might work for you.

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Line height RichTextBox

    Not tested, but:

    Get your known character's position in the RTB (.GetPositionFromCharIndex method)
    Get the line number that your character is on (.GetLineFromCharIndex method)
    Add 1 to give the next line
    Get the character index of the first character on that next line(.GetFirstCharIndexFromLine method)
    Get the position of that character within the RTB (.GetPositionFromCharIndex method)

    Subtracting the Y coordinates of the two position values should give the line height



    To get the height of the last line, I think you'll need to ensure the text always ends with a hard line break. Might be a problem.

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Line height RichTextBox

    For example, if you have a RichTextBox named RTB that contains rich text, add the following MouseUp event handler, and select a character on a line:
    Code:
    Private Sub RTB_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles RTB.MouseUp
        RTB.Refresh()
    
        Dim thisCharIndex As Integer = RTB.GetCharIndexFromPosition(e.Location)
    
        Dim lineHeight As Integer
    
        Dim thisCharPos As Point
        Dim thisCharLine As Integer
        Dim nextCharLine As Integer
        Dim nextCharIndex As Integer
        Dim nextCharPos As Point
    
    
        thisCharPos = RTB.GetPositionFromCharIndex(thisCharIndex)
        thisCharLine = RTB.GetLineFromCharIndex(thisCharIndex)
        nextCharLine = thisCharLine + 1
        nextCharIndex = RTB.GetFirstCharIndexFromLine(nextCharLine)
        nextCharPos = RTB.GetPositionFromCharIndex(nextCharIndex)
    
        lineHeight = nextCharPos.Y - thisCharPos.Y
    
    
        Using g As Graphics = RTB.CreateGraphics
            Dim rect As New Rectangle(0, thisCharPos.Y, RTB.Width, lineHeight)
            g.DrawRectangle(Pens.Black, rect)
        End Using
    
    End Sub
    It's bad code, only meant as a demonstration of the methods used.

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