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:
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:
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.