-2

My question is how I get selected text or marked text into the clipboard (ex. user have selected some text in web and when he presses CTRL + A + S, what he got selected or marked get into the clipboard).

I already setup the keyboard hook and background running so now I need to get selected thing into the clipboard.

I tried to get the answer on web but almost 90% of them is for a textbox and i don't need it for a textbox.

Thank you for all your answers, -DF

1 Answers1

0

You can get the selected text of a TextBox through the property SelectedText.

You can put something on the clipboard using the static method:

Clipboard.SetText(textBox1.SelectedText);

And get text from the clipboard (also the one copied into the clipboard in the browser) with:

if (Clipboard.ContainsText()) {
    textBox1.Text = Clipboard.GetText();
}

See: Clipboard Class.


If you only want to do that on the current form, you can get the current control with the form property ActiveControl. But will have to use different methods for different types of controls in order to get the selected text. Controls deriving from TextBoxBase have a SelectedText property. All controls have a Text property. All controls deriving from ListControl (like ComboBox) have a SelectedValue property. ComboBox has a SelectedItem property.

As you can see, you will have to make experiments and find the strategy which works best.


Also have a look into the Object Browser of Visual Studio, especially into the namespace System.Windows.Forms.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188