Pasting of serialnumber over multiple textfields

醉酒当歌 提交于 2019-12-13 19:21:21

问题


In my application users can upgrade their product (from trial to full) using a 32 characters serialnumber.

To make it as user friendly as possible for my (paying) customers I would like to be able to copy and paste the serial.

I want my customers to place the cursor in the first field under license and when the user pastes the 32 chars license I want it to fill all the fields.

I don't know where to start so if you can point me in the right direction that would be great.


回答1:


In the first textbox, I would put a large limit.

On the 'text changed', check the length. If the change is greater than 4 (your maximum). Delete the extra stuff and spread it over your textboxes.

If you copy-paste, it'll text change of 32, and it would work. You could also change the cursor (I think its .Focus() but I could be wrong) so it automatically 'hops' between the boxes.




回答2:


You could just hook up into the text changed event of the first textbox, and trim & split the pasted text into, groups of 4, and set the text of the other textboxes.

Pretty straightforward, and should "just work".




回答3:


You can override WndProc to capture the paste event (Windows message). Then simply take the pasted text, and copy into the textboxes. Full example, heavily inspired by this answer:

using System;
using System.Linq;
using System.Windows.Forms;

namespace SOPasteTextBox
{
    public class ClipboardEventArgs : EventArgs
    {
        public string ClipboardText { get; set; }
        public ClipboardEventArgs(string clipboardText)
        {
            ClipboardText = clipboardText;
        }
    }

    class PasteAwareTextBox : TextBox
    {
        public event EventHandler<ClipboardEventArgs> Pasted;

        private const int WM_PASTE = 0x0302;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_PASTE)
            {
                var evt = Pasted;
                if (evt != null)
                {
                    evt(this, new ClipboardEventArgs(Clipboard.GetText()));
                }
                return;
            }

            base.WndProc(ref m);
        }
    }

    static class Program
    {
        private static PasteAwareTextBox[] _textBoxes;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var mainForm = new Form();
            _textBoxes = Enumerable.Range(0, 8).Select(x => new PasteAwareTextBox() {Top = x*20}).ToArray();
            _textBoxes[0].Pasted += DoPaste;
            foreach (var box in _textBoxes)
            {
                mainForm.Controls.Add(box);
            }
            Application.Run(mainForm);
        }

        private static void DoPaste(object sender, ClipboardEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(e.ClipboardText))
                return;

            int i = 0;
            var text = e.ClipboardText.Split('-').Take(_textBoxes.Length);
            foreach (string part in text)
            {
                _textBoxes[i++].Text = part;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/8028862/pasting-of-serialnumber-over-multiple-textfields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!