1

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I was tempted to just answer "Yes". – driis Nov 06 '11 at 17:29
  • 1
    I've never seen the benefit of separate boxes. IMO just use one box; no more problem. Perhaps add dashes automatically during manual entry (or indeed automatic/paste). – Marc Gravell Nov 06 '11 at 17:29
  • This question - http://stackoverflow.com/questions/1156975/copy-paste-event-listener-in-c-sharp has some information about setting up a clipboard event listener in c#. It looks quite involved though. – ipr101 Nov 06 '11 at 17:32
  • @Marc Gravell: If you have never seen a benifit of separate boxes. Try readability for the user. – PeeHaa Nov 06 '11 at 17:34
  • I do second Marc's opinion. I like it the way several programs do it by adding hyphens automatically. E.g. the text field on http://join.me even does this using JavaScript. – Uwe Keim Nov 06 '11 at 17:39
  • @UweKeim: noted. However let's focus on my question in stead of changing the topic :D – PeeHaa Nov 06 '11 at 17:41
  • possible duplicate of [Override Paste Into TextBox](http://stackoverflow.com/questions/7852509/override-paste-into-textbox) – Hans Passant Nov 06 '11 at 17:59
  • 3
    @PeeHaa sometimes you need to think outside the TextBox :) – Marc Gravell Nov 06 '11 at 18:21
  • On a more serious note - I've entered plenty of serial numbers over the years. The split ones are worst. Sometimes, they'll be almost as convenient as a single box. Most times they are simply horrendous from a usability perspective. The usability claim holds no water, IMO. – Marc Gravell Nov 06 '11 at 18:24
  • @MarcGravell: last thing I want to say about this issue: The textfields act exactly like if it were 1 textfield. `Sometimes, they'll be almost as convenient as a single box` that's mine :) – PeeHaa Nov 06 '11 at 18:30

3 Answers3

4

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.

Haedrian
  • 4,240
  • 2
  • 32
  • 53
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".

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
2

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;
            }
        }
    }
}
Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341