• 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

    • Because it's just a one liner fix. One person ( StartAllBack dev ) was able to fix it... in a cave!
    • I have a first draft of the setup script available here if anybody is curious.  It'll work well enough for me, but obviously adapt it to your own needs as necessary. Link: https://gitlab.com/-/snippets/4876568
    • You make it sound like Microsoft’s success was inevitable—like they just coasted to the top because the system is broken. But that ignores how deep in the gutter Microsoft was. A bit more than a decade ago, Microsoft was a bloated, boring giant—universally hated by developer communities and seen as yesterday’s villain. It was well on its way to becoming the next IBM: slow, corporate, irrelevant. The company had a horrible public image, a toxic internal culture, and leadership plagued by stagnation. It was losing the browser war, failing at mobile, and completely missing the open-source wave—pouring good money after bad into battles it had already lost, playing an impossible catch-up game. Even tech enthusiasts openly wished for its demise. Microsoft’s comeback was nothing short of a miracle. Most companies that size, once caught in that kind of death spiral, never climb back out. But Microsoft reinvented itself—against all odds.
    • Samsung's profit nosedives again as chip division bleeds cash by Paul Hill Samsung has shared its second quarter financials for 2025. Unfortunately for the company, its operating profit plummeted to KRW 4.7 trillion, a sharp decline from KRW 10.4 trillion in 2Q24 and KRW 6.7 trillion in 1Q25. The Korean smartphone giant also recorded a significant drop of net profit to KRW 5.1 trillion in the second quarter, down from KRW 9.8 trillion in 2024 and KRW 8.2 trillion in the first quarter. Finally, overall revenue for the second quarter was KRW 74.5 trillion, a decrease from KRW 79.1 trillion in the first quarter. These figures are based on the consolidated financial statements that have been shared before the external review is completed, so some parts could change once it’s done. The worsening of performance doesn’t seem to be a Samsung issue, but rather a wider industry issue as Intel and LG Electronics have also seen poorer results in the second quarter. The Device Solutions (DS) division, which includes Memory and System LSI/Foundry, saw a big fall in operating profits to KRW 0.4 trillion in 2Q25 from KRW 6.5 trillion in 2Q24 and KRW 1.1 trillion in 1Q25. Despite an 11% increase in sales quarter-over-quarter for the Device Solutions division, profits were severely affected by one-off costs such as inventory value adjustments. The company said that its Foundry earnings were weak due to inventory value adjustments arising from US export restrictions on advanced AI chips to China and prolonged low utilization at mature nodes. Samsung’s Mobile eXperience (MX/NW) division maintained double-digit profitability and grew both its revenue and operating profit year-over-year. The Samsung Display Corporation (SDC) saw revenue increase from new smartphone models and growth in IT/Auto segments, and Harman also improved profitability with increased audio sales and cost optimizations. On the flipside, smartphone shipments fell compared to the first quarter when new models were released. However, good sales of the S25 series, A series, and tablets contributed to year-over-year growth. The Visual Display (VD) division saw earnings decline due to intensified competition despite improved premium sales mix. Going forward, Samsung wants to focus on improving Exynos competitiveness for its 2026 flagship lineup and expand sales of advanced sensors. It also said that its Foundry business will ramp up mass production of a new mobile System-on-Chip (SoC) with the GAA 2nm processor and improve factory utilization. The Mobile eXperience division is also looking to achieve solid profitability by reinforcing AI In tablets and wearables. It’s also planning to launch new form-factor products like XR and TriFold. With US trade tariffs not expected to go anywhere in the near future, Samsung has acknowledged that its Harman and Visual Display/Digital Appliances divisions will be under added pressure but plans to mitigate the impact through its global manufacturing footprint. Source: Samsung | Image via Depositphotos.com
    • I switched my bash script over to using Chromium instead of Edge and it's a much better experience.  Supports my bluetooth XBox controller for gameplay and menu navigation and launched the first time with no nonsense, no popups, nothing.  It just worked.
  • Recent Achievements

    • Week One Done
      whiloh earned a badge
      Week One Done
    • Week One Done
      memnoch earned a badge
      Week One Done
    • First Post
      UAVXP earned a badge
      First Post
    • Dedicated
      Xinotema earned a badge
      Dedicated
    • Rookie
      MrNukes went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      658
    2. 2
      ATLien_0
      205
    3. 3
      Xenon
      133
    4. 4
      neufuse
      125
    5. 5
      Michael Scrip
      123
  • Tell a friend

    Love Neowin? Tell a friend!