No Idea For Name
No Idea For Name

Reputation: 11577

find and set the location of indicator in textBox

I have a program with a TextBox that when I get a spacial input, I need to edit the text.

So I did that, but then the indicator returned to the beginning of the text, which is not a good MMI.

I was wondering if there is a way to edit the text without changing the indicator position, or is there a way to set the indicator position in the TextBox?

I've tried to edit the text in several ways, but they all lead to the indicator getting back to the start of the text.

I couldn't find a way to influence the location of the indicator so I'm guessing that's impossible, but I might be wrong.

please help.

Upvotes: 0

Views: 731

Answers (2)

Sandy
Sandy

Reputation: 11687

Try this.

int startPosition = textBox1.SelectionStart;
int selectionLength = textBox1.SelectionLength;

//do what ever you need
textBox1.Text = textBox1.Text.ToUpper();

//place cursor back in position
textBox1.Select(startPosition, selectionLength);

Hope it helps.

Upvotes: 2

DonBoitnott
DonBoitnott

Reputation: 11025

Try setting the selection. This would set the cursor to the 5th position, but select nothing:

textBox1.Select(5, 0);

Just make sure this is the last thing that happens before returning control to the user. Perhaps accompanied by a call to Focus if necessary.

In your case, you'd also need to keep track of what that position should be...prior to added/modified text? After it? Just replace 5 with that number.

Upvotes: 1

Related Questions