Winforms RichTextBox: How can I determine how many lines of text are visible?

前端 未结 2 768
独厮守ぢ
独厮守ぢ 2021-01-11 17:10

I have a Winforms app containing a RichTextBox.

How can I determine how many lines of text are displayed, currently visible?

Reason: I want to scroll th

2条回答
  •  自闭症患者
    2021-01-11 17:24

    To display the number of lines that a RichTextBox is capable of displaying, even if there is no text there currently, try:

    Dim s As SizeF = TextRenderer.MeasureText("A", rtb.Font, rtb.Size, TextFormatFlags.WordBreak)
    Dim letterHeight As Integer = CInt(s.Height)
    Dim displayableLines As Integer = rtb.Height / letterHeight
    

    This uses the height of a test letter 'A' in the current font to find the line height in pixels.

提交回复
热议问题