我正在做 C#/.NET 应用程序。我想在工具栏上创建一个按钮,基本上会调用 Ctrl+C (复制到剪贴板)。我查看了 Clipboard 类,但问题是因为我在表单上有多个文本框,我需要扫描哪个文本框具有焦点以及是否选定文本,以便从中选择文本等,所以我认为必须有我“一行”解决方案。
有任何想法吗?
(另外,如何在相同条件下将所有 3 个:剪切、复制、粘贴到工具栏添加 - 主窗体上的多个 tekstboxes..)
Edit: 如果是 Winforms..
将其放入您的调用函数中:
Clipboard.SetText(ActiveControl.Text);
正如 Daniel Abou Chleih 在下面提到的:如果您必须与控件交互才能调用焦点功能
将更改为该控件。仅当您通过其他方式调用它时,这才有效。
Edit:
不是单行代码,但适用于最后一个活动的文本框:
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);
}
现在您可以调用 CopyActiveText() 来获取最近失去焦点或当前获得焦点的 TextBox。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)