gpt4 book ai didi

c# - 粘贴到多个文本框中

转载 作者:太空宇宙 更新时间:2023-11-03 17:44:17 26 4
gpt4 key购买 nike

我有一个 .net 应用程序,其中包括搜索屏幕,该屏幕有一个面板,其中包含三个文本框,每个文本框的字符长度各不相同。

我想做的是捕获粘贴命令从第一个框中调用并将我的剪贴板粘贴到三个箱子。

此功能类似于许多接受序列号和电话号码输入的现代应用程序。

最佳答案

据我所知,除了捕获 WM_PASTE 事件外,没有其他明智的方法可以做到这一点。

从 TexBox 派生一个类并实现这个方法:

using System.Windows.Forms;
using System.ComponentModel;

class TextBoxWithOnPaste : TextBox
{

public delegate void PastedEventHandler();

[Category("Action")]
[Description("Fires when text from the clipboard is pasted.")]
public event PastedEventHandler OnPaste;

protected override void WndProc(ref Message m)
{
if (m.Msg == 0x302 && OnPaste != null) // process WM_PASTE only if the event has been subscribed to
{
OnPaste();
}
else
{
base.WndProc(ref m);
}
}
}

然后将其中三个自定义控件放在您的表单上,并将所有三个文本框上的 OnPaste 事件分配给相同的方法,在本例中我将其称为 textPasted():

private void textPasted()
{
String input = Clipboard.GetText();

int l1 = textBoxWithOnPaste1.MaxLength;
int l2 = textBoxWithOnPaste2.MaxLength;
int l3 = textBoxWithOnPaste3.MaxLength;

try
{
textBoxWithOnPaste1.Text = input.Substring(0, l1);
textBoxWithOnPaste2.Text = input.Substring(l1, l2);
textBoxWithOnPaste3.Text = input.Substring(l2, l3);
}
catch (Exception)
{ }

}

因为你暗示“像连续剧”,我猜你希望粘贴的字符串在文本框之间分开。上面的代码对此并不完美(尝试在所有三个文本框中手动输入数据后将一个空格粘贴到第三个文本框中,因此如果您知道文本粘贴在哪个文本框中,例如通过更改事件的参数,那就太好了并以这种方式发送发件人),但它基本上可以工作,我想你可以弄清楚其余部分(你可以使用 Tag 属性来识别文本框)。

关于c# - 粘贴到多个文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com