1

I am currently trying to make my own version of notepad using VB.net 2008 Winforms and I have a problem in making my 'Go To' feature. To explain the situation, I have a 'Go to' sub menu wherein if I type Go to line 8 and hit OK..the blinking cursor should go to line 8 of the multiple line text box. I have implemented a code to know where the current line and column of the cursor but I don't know how to reverse that code so that i can command the blinking cursor to move in that specified line in the multiline textbox..

Code to identify current line and column:

   Public Sub linecol()
    Try
        xid = txtpad.SelectionStart
        linenum = txtpad.GetLineFromCharIndex(xid)
        xpoint = txtpad.GetPositionFromCharIndex(xid)
        xpoint.X = 0
        colnum = xid - txtpad.GetCharIndexFromPosition(xpoint)
        linenum += 1
        colnum += 1

        stat.Text = "Ln " & linenum.ToString & ", Col " & colnum.ToString
    Catch ex As Exception
        Me.Cursor = Cursors.Default
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try

And my problem is how to go to a specify line in that textbox.like going to line 8...

user3610167
  • 13
  • 1
  • 5

2 Answers2

0

From this question:

Private Function GetNthIndex(s As String, t As Char, n As Integer) As Integer
    Dim count As Integer = 0
    For i = 0 To s.Length - 1
        If s(i) = t Then
            count += 1
            If count = n Then
                Return i
            End If
        End If
    Next
    Return -1
End Function

and use it like this:

Me.TextBox1.SelectionStart = GetNthIndex(Me.TextBox1.Text, Chr(10), rowNumber - 1) + 1
Community
  • 1
  • 1
Alin I
  • 580
  • 1
  • 7
  • 24
0

You want to use the below to set the position of the cursor

      textBox.Focus();
      textBox.SelectionStart = 2;
      textBox.SelectionLength = 0;

To find the SelectionStart index you can use String.IndexOf method to find the position new line characters (Char:10) of the content.

Using the above you can determine the index of new line n and set the cursor position immediately after it.

Sam Makin
  • 1,526
  • 8
  • 23