Pasting into multiple text boxes(粘贴到多个文本框中)
问题描述
我有一个 .net 应用程序,其中包括搜索屏幕,该屏幕有一个面板,其中有 三个文本框,每个文本框都有不同的字符长度.
I have a .net application which includes search screen which has a panel with has three text boxes, each with a varying character lengths.
我想做的是 capture 当 paste 命令 当 invoked 从第一个框并将我的剪贴板粘贴到三个盒子.
What I'd like to do is capture when the paste command when invoked from the first box and paste my clipboard into the three boxes.
此功能类似于许多接受输入序列号和电话号码的现代应用程序.
This functionality is similar to many modern applications accepting input for serial keys and phone numbers.
推荐答案
据我所知,除了捕获 WM_PASTE 事件之外,没有其他明智的方法可以做到这一点.
As far as I can find there is no other sensible way of doing this than to capture the WM_PASTE event.
从 TexBox 派生一个类并实现此方法:
Derive a class from TexBox and implement this method:
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()
:
Then put three of those custom controls on your form, and assign the OnPaste
event on all three textboxes to the same method, in this case I called it 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
属性来标识文本框).
Since you implied "like a serial", I guessed you want the pasted string to be split among the textboxes. The code above is not perfect for that (try pasting a single space into the third text box after entering data manually in all three, so it would be nice if you knew in which textbox the text was pasted, for example by altering the event's parameters and that way sending the sender with it), but it basically works and I guess you can figure out the rest (you could use the Tag
property to identify the textbox).
这篇关于粘贴到多个文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:粘贴到多个文本框中
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 使用 rss + c# 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01