Pasting into multiple text boxes

2019-01-29 01:53发布

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.

5条回答
Animai°情兽
2楼-- · 2019-01-29 02:09

As far as I can find there is no other sensible way of doing this than to capture the WM_PASTE event.

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

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

}

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-29 02:10

You could increase the character limit of the boxes and register for TextChanged and if the pasted (or typed) text is longer jump/cut to the next TextBox.

查看更多
▲ chillily
4楼-- · 2019-01-29 02:20

you can get the captured text String txt = Clipboard.GetText(); and place it in the "Text" property of the other textbox

查看更多
Emotional °昔
5楼-- · 2019-01-29 02:26

Capture the paste event:

protected override void WndProc(ref Message m) {
    // Did paste occur?
    if (m.Msg == 0x302) {
        //Paste occurred, add your logic here
    }
    base.WndProc(ref m);
}

Then, access the Clipboard object to get the desired text.

查看更多
男人必须洒脱
6楼-- · 2019-01-29 02:26

You can bind key down event, and when you get Ctrl + V or Ctrl + v, you update the value of the three textbox with value in the clipboad. You can do this on TextChanged event off first text box.

查看更多
登录 后发表回答