• 0

[vb .net] Go to Line in textbox/richtextbox


Question

Can anyone tell me the general idea of how to implement a "go to line number" in a textbox in vb .net? I know how to get the current line number, but I really can't figure out how to put the cursor at the beginning of a line number entered into a dialog. I found one example online, but it relied on the assumption that word wrap would be off which doesn't work in my case.

Any help would be appreciated.

14 answers to this question

Recommended Posts

  • 0

Public Sub [GoTo](ByVal ln As Integer)
  Win32.LockWindowUpdate(rtb.Handle)
  SelectionStart = Win32.SendMessage(rtb.Handle, Win32.EM.LINEINDEX, ln - 1, 0)
  Win32.LockWindowUpdate(IntPtr.Zero)

rtb.Focus()
End Sub

the lockwindowupdate calls aren't necessary, but they do help prevent flicker

Edited by Ianmac45
  • 0

I don't understand that either. I did it like this

Public Class TextBox2
    Inherits TextBox

    Public Property LineNumber()
        Get
            'i dunno
            Return 0
        End Get
        Set(ByVal value)
            Me.SelectionStart = 0

            For i As Integer = 1 To value
                SendKeys.Send("{DOWN           Next i
        End Set
    End Property

End Class

  • 0
  elliot said:
Me.SelectionStart = 0

I reset it the position to the top left. :)

586224601[/snapback]

Hmm, I guess that would work. I'm just worried about the execution time on a large file. The other day I was working on files from Firefox that are 10,000+ lines long and realized that finding line 5904 can be difficult. With your code, it could take a while to get to the line.

  • 0

mine does work

just include the lockwindowupdate and sendmessage api calls

i thought you guys would be able to figure it out, i guess not

Public Declare Auto Function LockWindowUpdate Lib "user32" (ByVal handle As IntPtr) As Boolean

Public Declare Auto Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal handle As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

  • 0

Ianmac45's will work fine, you just need to import the SendMessage and LockWindowUpdate (actually I've never used LockWindowUpdate before so I assume it's right) functions manually as Ianmac45 didn't include the Win32 unit.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.