raaga
raaga

Reputation: 81

Add a text in the cursor position in an textbox in vb.net

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

Answers (3)

Odinsonnah
Odinsonnah

Reputation: 73

Turns out there's a really easy way to do this.

TextBox1.SelectedText = "<br>"

Upvotes: 5

bla
bla

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

Konrad Rudolph
Konrad Rudolph

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

Related Questions