• 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.
  • Posts

    • I have YT subscription and Spotify but can you find rarer tunes on YT?
    • If they added ads I would dump it in a heartbeat, that's the whole reason I got the subscription to get rid of ads after every 2 tunes
    • That's if your company makes use of that separation. My company just uses the Intune portal for the authentication but doesn't utilize the separation, it just locks down the normal outlook and other Microsoft apps so you have to enter pin or biometric every time you open the apps, even for the personal sides.
    • Make your PC faster with this outstanding 1TB NVMe Gen5 SSD deal by Sayan Sen If you are not quite happy with how fast your PC boots up or how quick and snappy things feel, then MSI may have just the right thing for you. The company's SPATIUM M560 NVMe SSD is currently on sale for a price of just $70 for the 1TB model making it an amazing deal (purchase link under the specs table down below). The drive is based on 218-layer 3D TLC NAND and that means it has quite good endurance, even being a 1TB model. As it is a PCIe Gen5 NVMe SSD it promises very fast delivery speeds, but it does get quite hot and thus, adding a decent heatspreader is compulsory or else it will throttle under sustained loads which is common for PCIe 5.0 drives. The one minor drawback of the model is the lack of DRAM cache on it, however, it makes use of HMB (host memory buffer) technology that can access system memory for metadata caching purposes which can improve random access times. The technical specifications of the MSI SPATIUM M560 1TB are given below: Specification Value Controller Phison PS5031-E31T NAND Flash 218-layer Kioxia BiCS8 Sequential Read Up to 10,200 MB/s Sequential Write Up to 8,400 MB/s Random Read (4 KB) Up to 1,300,000 IOPS Random Write (4 KB) Up to 1,400,000 IOPS Operating Temperature 0 °C – 70 °C Storage Temperature –40 °C – 85 °C Endurance (TBW) 600 TBW MTBF Up to 1,500,000 hours Get it at the link below (comes with a five-year warranty): MSI SPATIUM M560 PCIe 5.0 NVMe M.2 1TB Portable SSD, Black: $69.99 (Sold and Shipped by Amazon US) If you want a 2TB option, check this Corsair MP700 Elite for $170. This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Only affects windows 10, which hits EOL in 2 months. This change is unimportant, go upgrade that potato pc.
  • Recent Achievements

    • Week One Done
      Tayloravila earned a badge
      Week One Done
    • First Post
      Axl917 earned a badge
      First Post
    • Week One Done
      pirateshiptours earned a badge
      Week One Done
    • Week One Done
      Zojaji earned a badge
      Week One Done
    • First Post
      Soeaker4thedead earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      731
    2. 2
      ATLien_0
      184
    3. 3
      +FloatingFatMan
      151
    4. 4
      Xenon
      115
    5. 5
      wakjak
      113
  • Tell a friend

    Love Neowin? Tell a friend!