简体   繁体   中英

windows form applications' cut copy paste functionality

I have windows forms application with multiple forms and controls in them. I want if user has selected some text in any control of any form of my application and click on cut/copy/paste button on toolbar operation get performed accordingly.

im using C#.net's sendkeys.send("^c") on click of copy button but it doesn't work...

OR any 1 can tell if is there any way to get selected text (despite of knowing, which form/control of my application).

Thanks in advance...

have you used clipboard to copy and paste you data if not than use clipboard for this

check this article for more about clipboard: http://www.geekpedia.com/tutorial188_Clipboard-Copy-and-Paste-with-Csharp.html

I use this in the method handling the copy event:

if (this.ActiveControl is TextBox)
{
      Clipboard.SetDataObject(((TextBox)this.ActiveControl).SelectedText, true);
}
if (this.ActiveControl is RichTextBox)
{
      Clipboard.SetDataObject(((RichTextBox)this.ActiveControl).SelectedText, true);
}
if (this.ActiveControl is ComboBox)
{
       Clipboard.SetDataObject(((ComboBox)this.ActiveControl).SelectedText, true);
}

For paste, something like this:

nCursorPosition = ((RichTextBox)this.ActiveControl).SelectionStart;
this.ActiveControl.Text = this.ActiveControl.Text.Insert(nCursorPosition, Clipboard.GetText());

To your second question:

You can use this solution What is the preferred way to find focused control in WinForms app? to find the currently focused control.

Then check, what type it is to read the selection (ie if it is TextBox use SelectedText -Propery http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.selectedtext.aspx )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM