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

3

25

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!

Opia answered 23/8, 2012 at 15:17 Comment(6)
How do you key in manually if the textbox never gets focus?Sponge
possible duplicate of How to keep WPF TextBox selection when not focused?Single
@Blam : I set the text of the textbox.Opia
@H.B. : I already checked it. The problem is that my textbox never gets focus. The solution in the thread you mentioned is for textboxes that lose focus, mine never gets one, so it never raises the lostFocus() event. I probably need a way around te normal procedure of selecting and highlighting text.Opia
@user1340852: It's the same question though and that is all that matters in terms of duplicates.Single
@H.B. If I may, I disagree. Even if the title is the same, if the OP is asking a different question, it's not a duplicate. However, that they have the same title (or very similar) means that one (or both) of them could do a better job being more specific.Mesotron
L
18

You can use the following code to achieve your purpose:

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

Hope this helps.

List answered 21/10, 2012 at 15:30 Comment(1)
This is with focus, but question was - "without focus"Sacramentalist
S
16

Another alternative:

textBoxName.SelectAll();
Sweeney answered 18/3, 2013 at 13:45 Comment(0)
S
1

I really like this type of selection:

textbox.Focus();
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Lenght;
Struck answered 20/12, 2020 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.