如何得到RichTextBox的可视行数?

开始用下面的方法:
        Dim r As RichTextBox = RichTextBox1
        Dim lines, iFirst, iLast As Int32
        iFirst = r.GetLineFromCharIndex(r.GetCharIndexFromPosition(New Point(0, 0)))
        iLast = r.GetLineFromCharIndex(r.GetCharIndexFromPosition(New Point(0, r.Height)))
        lines = iLast - iFirst
        MessageBox.Show(lines)
但发现不是很准确,如果滚动条在中间的时候才是正确的行数,如果RichTextBox中没有字符,或字符不多,没有滚动条出现的时候就不对了,

下面的方法可以:

       Dim g As Graphics
        g = RichTextBox1.CreateGraphics()
        lines = RichTextBox1.Height / g.MeasureString("test",RichTextBox1.Font).Height()
        MessageBox.Show(lines)

 

你可能感兴趣的:(study,road)