Balachandran
Balachandran

Reputation: 87

How to handle textbox selection start property in vb.net or c#

When i am assigning text in to textbox that time the selection start property is zero so the cursor(caret) is moving to first location of the textbox. How to solve this problem?

Code Sample

Consider that the textbox already have text like 'Hello Developers'

    Dim CurPosition As Integer = TextBox1.SelectionStart
    'CurPosition=11 (i.e) SelectionStart is 11 (cursor position)'


    TextBox1.Text = TextBox1.Text.Remove(5, 11)
    'After Assigning Text the selectionStart is 0 (cursor position)'

    TextBox1.SelectionStart = TextBox1.TextLength
    'Here After the selectionstart is 5'

    TextBox1.ScrollToCaret()

so when i am executing this code the cursor move up and down so how to solve this problem.

Upvotes: 0

Views: 6685

Answers (2)

xoxox
xoxox

Reputation: 798

I think I don't really know what's your problem.

But I think you could assign the selectionstart-property after scrolltocaret() again or just use the select() method.

Upvotes: 1

pingoo
pingoo

Reputation: 2064

Do you mean that your the Caret dosnt appear within the text box? If so try:

 Dim CurPosition As Integer = TextBox1.SelectionStart
 'CurPosition=11 (i.e) SelectionStart is 11 (cursor position)'


 TextBox1.Text = TextBox1.Text.Remove(5, 11)
 'After Assigning Text the selectionStart is 0 (cursor position)'

 TextBox1.SelectionStart = TextBox1.TextLength
 'Here After the selectionstart is 5'

 TextBox1.ScrollToCaret()

 TextBox1.Focus()

Upvotes: 0

Related Questions