开发者

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

开发者 https://www.devze.com 2023-01-18 19:17 出处:网络
I need to add text at the end of th开发者_StackOverflowe cursor\'s position in VB.NET I tried: TextBox1.Text = TextBox1.Text.Insert(TextBox1.SelectionStart, \"<br>\")

I need to add text at the end of th开发者_StackOverflowe 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.


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


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

TextBox1.SelectedText = "<br>"


Add this code after your code to move the caret to the end of your textbox value:

TextBox1.SelectionStart = TextBox1.TextLength
0

精彩评论

暂无评论...
验证码 换一张
取 消