Reputation: 81
I need to add text at the end of the cursor's position in VB.NET I tried:
TextBox1.Text = TextBox1.Text.Insert(TextBox1.SelectionStart, "<br>")
It works but the cursor position still moves to the starting position.
Upvotes: 3
Views: 18322
Reputation: 73
Turns out there's a really easy way to do this.
TextBox1.SelectedText = "<br>"
Upvotes: 5
Reputation: 5480
Add this code after your code to move the caret to the end of your textbox value:
TextBox1.SelectionStart = TextBox1.TextLength
Upvotes: 0
Reputation: 545588
Just re-set the SelectionStart
property after assigning the text:
Dim insertText = "<br>"
Dim insertPos As Integer = TextBox1.SelectionStart
TextBox1.Text = TextBox1.Text.Insert(insertPos, insertText)
TextBox1.SelectionStart = insertPos + insertText.Length
Upvotes: 8