Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some way to determine if there is any selected text in the currently selected window.

I have a hook set up on the keyboard so that I can catch the then copy that into my own "clipboard", but how can I find out the text that is selected?
Posted
Updated 2-Jun-11 14:24pm
v2
Comments
Sanjay J Patolia 3-Jun-11 0:36am
   
Please share a sample code here. So that we can easily provide an answer. Thanks :)
Sanjay J Patolia 3-Jun-11 0:37am
   
I am not getting whether you are trying to fetch selected text for whole window or a particular control here. Thanks :)
Gluups 9-Apr-14 19:31pm
   
Hello, would anybody be so kind as to answer the question seriously ?
I wrote a program to launch a search on the selected text, that can be in Notepad, in a word processor, in a web browser, or in another application.
I first launch a copy by SendKeys.SendWait("^c"), then obtain the text by Clipboard.GetText();
This runs jolly good, but the sendkeys generates a HIPS alert, so I am looking for another way. I thought to the APIs, I tried SendMessage, specifically WM_GETTEXT, but this gives me the caption of the window, not the selected text. There is a EM_GETSELTEXT message, but this is ignored for notepad : EM stands for rich Edit Message. WM_COPY is ignored.
Any better idea ?

1 solution

You can determine this pretty easily by creating an if statment for .

SQL
if (textbox.SelectedText != "")
{
    your stuff
}


If you need to detect multiple objects on the same form you can list them with or operators in the condition.
   
Comments
Dalek Dave 2-Jun-11 20:25pm
   
Simples!
Tomwa Ivory 2-Jun-11 20:33pm
   
Glad I could help :) Now if only someone can help me with this bedamned XML lol :D
DominicZA 3-Jun-11 2:07am
   
Hmmm, yes, your answer is correct. However, I am trying to copy text that is selected in a totally different process! For example, Ill copy the selected text in Notepad
Tomwa Ivory 3-Jun-11 2:17am
   
What exactly do you need to copy from notepad for?
DominicZA 3-Jun-11 2:22am
   
Ok, but how do I know what is selected in their document??? I know that I can just create an instance of the clipboard class and just say set text, but how do I know what text to set?
Tomwa Ivory 3-Jun-11 2:28am
   
string stufftopase = "pastestuff"; //string variable containging test words to paste
string copyholder = Clipboard.GetText(); //Copys the text from the clipboard to the string variable copyholder
Clipboard.SetText(stufftopase); //Moves the data from the variable stufftopaste to the system clipboard

These lines should help you manage the clipboard if you need to managed highlighted text specifically then you'll need much more code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900