Is there a way to get the currently highlighted text in a certain textbox without being able to access the textbox (by just using the cursor)?
Asked
Active
Viewed 832 times
-1
-
Please elaborate. What kind of application do you have, what application contains the textbox you wish to access? What do you mean by "without access to the textbox"? Do you want it to work for any textbox, or are you looking for a specific one? Have you tried searching? See for example [How do I get the selected text from the focused window using native Win32 API?](http://stackoverflow.com/questions/2251578/how-do-i-get-the-selected-text-from-the-focused-window-using-native-win32-api). – CodeCaster Aug 26 '14 at 10:42
-
It's for a winforms application. I have several textboxes and would like to be able to get the text which is highlighted in any of them. By "without access to textbox" I mean that I would like to get the highlighted text without having to identify which textbox contains the highlighted text. I tried searching but didn't find anything relevant. – Ezz Aug 26 '14 at 10:48
2 Answers
1
If the controls are in your application, it's as easy as looping over the control collection, checking for a TextBox
and investigating each one's SelectedText
property.

CodeCaster
- 147,647
- 23
- 218
- 272
-
Yes, I considered this possibility, but isn't their a way to obtain the text by using only the cursor? – Ezz Aug 26 '14 at 10:58
-
Yes, see for example [How do I get the selected text from the focused window using native Win32 API?](http://stackoverflow.com/questions/2251578/how-do-i-get-the-selected-text-from-the-focused-window-using-native-win32-api). – CodeCaster Aug 26 '14 at 10:59
-
Thanks, first solution worked. This was also helpful to identify the textbox http://msdn.microsoft.com/en-us/library/system.windows.forms.control.containsfocus(v=vs.110).aspx – Ezz Aug 26 '14 at 11:39
1
Ended up doing something like this:
foreach (TextBox textBox in this.Controls.OfType<TextBox>()) {
if(textBox.ContainsFocus)
if (textBox.SelectedText != "") {
//
//
}
}

Ezz
- 534
- 2
- 8
- 17