粘贴到多个文本框 [英] Pasting into multiple text boxes

查看:269
本文介绍了粘贴到多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.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.

我想要做的是捕捉时,当从第一箱调用粘贴命令并粘贴剪贴板我进三框。

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)
    { }

}

既然你暗示像串,我猜你想要的文本框之间被分割粘贴的字符串。在code以上不适合的(尝试粘贴一个空间到第三个文本框在所有三个手动输入数据后,因此,如果您知道在文本框中的文本粘贴,例如通过改变这将是很好事件的参数,这种方式发送它的发送者),但它基本上是工作,我想你可以计算出其余的(你可以使用的 标签 属性标识文本框)。

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).

这篇关于粘贴到多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆