简体   繁体   English

windows表单应用程序的剪切复制粘贴功能

[英]windows form applications' cut copy paste functionality

I have windows forms application with multiple forms and controls in them. 我有Windows窗体应用程序,其中包含多个窗体和控件。 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... 我在点击复制按钮时使用C#.net的sendkeys.send(“^ c”)但它不起作用...

OR any 1 can tell if is there any way to get selected text (despite of knowing, which form/control of my application). 或任何1可以判断是否有任何方式来获取所选文本(尽管知道,我的应用程序的形式/控制)。

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 有关剪贴板的更多信息,请查看此文章: 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? 您可以使用此解决方案在WinForms应用程序中找到集中控件的首选方法是什么? 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 ) 然后检查,读取选择的是什么类型(即,如果它是TextBox使用SelectedText -Propery http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.selectedtext.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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