user1340852
user1340852

Reputation: 875

how to highlight/select text in a wpf textbox without focus?

I want to highlight selected text in a wpf textbox while the textbox is not focused. In my application, my textbox never gets focus, and every key input is done manually.

I was wondering if there is a way to highlight the selected text when the textbox is not focused?

Any help would be appreciated!

Upvotes: 25

Views: 30609

Answers (3)

sysOut
sysOut

Reputation: 419

I really like this type of selection:

textbox.Focus();
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Lenght;

Upvotes: 1

pdvries
pdvries

Reputation: 1382

You can use the following code to achieve your purpose:

textBoxToHighlight.Focus();
textBoxToHighlight.Select(0, textBoxToHighlight.Text.Length);

Hope this helps.

Upvotes: 19

usefulBee
usefulBee

Reputation: 9692

Another alternative:

textBoxName.SelectAll();

Upvotes: 16

Related Questions