Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have this application where I need to get the selected text from an other application. I do this by sending CTRL+C to the app and using the clipboard:

C#
private void button1_Click(object sender, EventArgs e)
{
    GetText();
}

private void GetText()
{
    try
    {
        Clipboard.Clear();
        SetForegroundWindow(Process.GetProcessesByName("app")[0].MainWindowHandle);
        SendKeys.SendWait("^c");
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex);
    }
}


All works fine, I get the text that I want.
BUT when I want to use the text at the same time that I'm grabbing it I seem to get a problem.

When I do:
C#
 private void button1_Click(object sender, EventArgs e)
 {
     GetText();
     DoSomething();
 }

private void DoSomething(){
     string str = Clipboard.GetText();
     ...
     ...
}


It seems that during the process of the button1_click the string is empty. Though in the end, when everything is done, my clipboard has the selected text.

So the clipboard only gets my selected text when button1_click is completed.

My solution now is to use 2 buttons: one to load the text in the clipboard, and one to do something with it.
This is not ideal, so I'm hoping there's a solution for my problem so I only have to use one button.
Posted
Updated 18-Apr-12 21:27pm
v2

1 solution

This is pretty simple - the problem is that you don't understand how things work!

When you send CTRL-C to a different process, it doesn't mean that it processes them immediately and returns. Instead the keys get queued into the message list for the process and will be handled when the application is good and ready - this may be when your Click event handler finishes, but it could also be several minutes later if the other application is particularly busy and / or badly written.

Besides that, transferring data between applications via the Clipboard is a very bad way to do things: if nothing else it destroys anything the user has put on the clipboard without them knowing. If I am working on a complex image, and I have just spent half an hour building a layer and putting it on the clipboard to paste into a different image, then your application comes along and bins it, then I am not going to be even slightly amused. Your app will be uninstalled so quickly that the EXE file will get friction burns!

Find a better way. This is a bad solution that produces unpredictable results.
 
Share this answer
 
Comments
LeonreyDragoon 19-Apr-12 3:38am    
Yeah... you got a point there. Thanks, I'll see for an other way to do this.
OriginalGriff 19-Apr-12 3:40am    
You're welcome!
What are you actually trying to do?
LeonreyDragoon 19-Apr-12 3:45am    
This part was for text-to-speech, problem being the other application is a QWidget (gets me confused).
This is an project I'm doing as internship, which is part of my final year at school.
Sergey Alexandrovich Kryukov 19-Apr-12 3:48am    
Good points! My 5.
--SA

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