[英]Copy text to Clipboard
I am doing C#/.NET app. 我在做C#/ .NET应用程序。 I want to make a button on toolbar that will basically invoke Ctrl+C (copy to clipboard).
我想在工具栏上创建一个按钮,它基本上会调用Ctrl + C(复制到剪贴板)。 I looked to Clipboard class, but problem is since I have multiple textboxes on form, I would need to scan which one has focus and if/is selected text, in order to select text from it etc., so I think there must me “one-liner” solution.
我看了Clipboard类,但问题是因为我在表单上有多个文本框,我需要扫描哪一个有焦点,如果/是选中文本,以便从中选择文本等,所以我认为必须有我“单线“解决方案。
Any ideas? 有任何想法吗?
(Also, how to add all 3: Cut, Copy, Paste to toolbar, under same conditions- multiple tekstboxes on main form..) (另外,如何在相同的条件下添加所有3:剪切,复制,粘贴到工具栏 - 主窗体上的多个tekstbox ..)
Edit : If for Winforms.. 编辑 :如果是Winforms ..
Place this in your invoke function: 将它放在你的调用函数中:
Clipboard.SetText(ActiveControl.Text);
As mentioned below by Daniel Abou Chleih: If you have to interact with a control to invoke the function the focus will be changed to that control. 正如Daniel Abou Chleih所述:如果你必须与控件交互以调用该函数,焦点将被更改为该控件。 This only works if you call it through other means.
这仅在您通过其他方式调用时才有效。
Edit : Not a one-liner but works on the last active TextBox: 编辑 :不是单行,但适用于最后一个活动的TextBox:
private Control lastInputControl { get; set; }
protected override void WndProc(ref Message m)
{
// WM_SETFOCUS fired.
if (m.Msg == 0x0007)
{
if (ActiveControl is TextBox)
{
lastInputControl = ActiveControl;
}
}
// Process the message so that ActiveControl might change.
base.WndProc(ref m);
if (ActiveControl is TextBox && lastInputControl != ActiveControl)
{
lastInputControl = ActiveControl;
}
}
public void CopyActiveText()
{
if (lastInputControl == null) return;
Clipboard.SetText(lastInputControl.Text);
}
Now you can call CopyActiveText() to get the most recent TextBox that lost focus last or currently has focus. 现在,您可以调用CopyActiveText()来获取最近失去焦点或当前具有焦点的最新TextBox。
If you are using WinForms I possibly have a small Solution for that problem. 如果你使用WinForms我可能有一个小问题的解决方案。
Create an Object to store your last selected TextBox 创建一个Object来存储上次选择的TextBox
TextBox lastSelectedTextBox = null;
In your Constructor create an Eventhandler for each TextBox
in your Form
for the GotFocus
-Event by calling the AddGotFocusEventHandler
-Method with the parameter this.Controls
. 在构造函数中,通过使用参数
this.Controls
调用AddGotFocusEventHandler
-Method,为Form
中的每个TextBox
为GotFocus
-Event创建一个Eventhandler。
public void AddGotFocusEventHandler(Control.ControlCollection controls)
{
foreach (Control ctrl in controls)
{
if(ctrl is TextBox)
ctrl.GotFocus += ctrl_GotFocus;
AddGotFocusEventHandler(ctrl.Controls);
}
}
And set the lastSelectedTextBox
to your currently selected TextBox 并将
lastSelectedTextBox
设置为当前选定的TextBox
void c_GotFocus(object sender, EventArgs e)
{
TextBox selectedTextBox = (TextBox)sender;
lastSelectedTextBox = selectedTextBox;
}
In your Click-EventHandler for the button check if selectedText is null and copy the text to clipboard: 在Click-EventHandler中,按钮检查selectedText是否为null并将文本复制到剪贴板:
private void Button_Click(object sender, EventArgs e)
{
if(String.IsNullOrWhiteSpace(lastSelectedTextBox.SelectedText))
Clipboard.SetText(lastSelectedTextBox.Text);
else
Clipboard.SetText(lastSelectedTextBox.SelectedText);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.