windows form applications' cut copy paste functionality
I have windows forms application with multiple forms and controls in th开发者_Python百科em. 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.
i m 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 (i.e. if it is TextBox
use SelectedText
-Propery http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.selectedtext.aspx)
精彩评论