Get RichTextBox Status Information

This example demonstrates a simply way to get information from a RichTextBox that is often displayed in a status bar (ie number of lines, current line, column etc). This code uses API calls in some cases, and expects the RichTextBox to be called rtfMain.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const EM_LINEINDEX = &HBB

Public Property Get LineCount() As Long
    LineCount = SendMessage(rtfMain.hwnd, EM_GETLINECOUNT, 0&, 0&)
End Property

Public Property Get GetFirstLineVisible() As Long
    GetFirstLineVisible = SendMessage(rtfMain.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
End Property

Public Property Get CurrentColumn() As Long
Dim lCurLine As Long
    ' Current Line
    lCurLine = 1 + rtfMain.GetLineFromChar(rtfMain.SelStart)
    ' Column
    CurrentColumn = SendMessage(rtfMain.hwnd, EM_LINEINDEX, ByVal lCurLine - 1, 0&)
    CurrentColumn = (rtfMain.SelStart) - CurrentColumn
End Property

Public Property Get CurrentLine() As Long
    CurrentLine = 1 + rtfMain.GetLineFromChar(rtfMain.SelStart)
End Property

You might also like...


James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The trouble with programmers is that you can never tell what a programmer is doing until it's too late.” - Seymour Cray