Tag Archives: textbox line number

Getting the Current Line Number in Textbox using VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

There is unfortunately no so-called ‘built-in’ way to get the current line number in a Visual Basic.NET Textbox control. Microsoft added that support in Visual Basic 2005/2008/2010. But many still use the original .NET versions which do not have that feature in the textbox control.

What to do? Well, there is a somewhat easy way to add this feature by using the Windows API. The API to use is the SendMessage API call. Below is the declaration, constant value, and line# variable…


Getting the Current Line Number code

    '
    'Unfortunately, .NET before 2005 doesn’t have a built-in feature to get the current
    'line. So I am using the SendMessage API to do it. You CAN do various extractions or
    'whatever to do it, but this is easier and more elegant in my opinion.
    '
    'Used to Send Messages to the control and will be used with a request for the
    'current line number in the textbox control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, _
        ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    '
    'Constant found in the VB 6.0 API Viewer App IT will be passed to the SendMessage
    'API to tell the control to give the current Line # from the textbox character position.
    Public Const EM_LINEFROMCHAR = &HC9
    '
    'Will be used to contain the current line #.
    Dim lineNum As Int32

You can put the codes below in the textbox_Click, textbox_Keydown, textbox_TextChange event you want to take place or a button control  to get the line# results or whatever. In other words, put the code for where/when you want the current line results.

I put these codes in the Textbox_TextChanges event and used a label control to display the current line number. Its also worth nothing that Windows reports line # starting at “0”. So if the API returns a “0”, you can Add (+) one to the value which will then show the line# as 1. Another example is if the Windows API returns a 1, but in reality the current line is the second, then as before simple add (+) a One(1) to the value which will then be line #2.

        '
        'Call the API to get the Line # from the current char. Txt is your textbox control.
        lineNum = SendMessage(txt.Handle.ToInt32, EM_LINEFROMCHAR, -1, 0)
        '
        'Windows sees the first line in a edit box as line 0. So just add a value of 1 to the
        'result if you want the first line to be line #1.
        lblCurrent.Text = "Current Line: #" & (lineNum + 1).ToString
        '
        'Check for line count first to keep a possible error from happening if the textbox has
        'none.
        If txt.Lines.Length > 0 Then
            '
            'Get the text at the current line position.
            txtLineText.Text = txt.Lines.GetValue(lineNum)

        End If


Thats all there is to it! When you run your project and type your characters or make a new line or whatever in the textbox, the label control should show the number that the caret or cursor is currently at. If you want the Line # code to be updated when the mouse clicks on a different line or position, simply put the code above in the textbox_Click event. Have Fun!

Jason

Revisited: 2015