Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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;
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top