将序列号粘贴到多个文本字段上

发布于 2024-12-13 20:30:23 字数 195 浏览 2 评论 0原文

在我的应用程序中,用户可以使用 32 个字符的序列号升级他们的产品(从试用版到完整版)。

为了使其对我的(付费)客户尽可能友好,我希望能够复制并粘贴序列号。

我希望我的客户将光标放在许可证下的第一个字段中,当用户粘贴 32 个字符的许可证时,我希望它填充所有字段。

我不知道从哪里开始,所以如果你能指出我正确的方向,那就太好了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想念有你 2024-12-20 20:30:23

在第一个文本框中,我会设置一个很大的限制。

在“文本已更改”上,检查长度。如果变化大于 4(您的最大值)。删除多余的内容并将其分布在文本框中。

如果你复制粘贴,它会将文本更改为 32,并且它会起作用。您还可以更改光标(我认为它是 .Focus() 但我可能是错的),以便它自动在框之间“跳跃”。

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.

心碎无痕… 2024-12-20 20:30:23

您只需连接到第一个文本框的文本更改事件,然后修剪和修剪即可。将粘贴的文本分成 4 组,并设置其他文本框的文本。

非常简单,并且应该“正常工作”。

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

妄想挽回 2024-12-20 20:30:23

您可以重写 WndProc 以捕获粘贴事件(Windows 消息)。然后只需将粘贴的文本复制到文本框中即可。完整示例,很大程度上受到这个答案的启发:

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

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;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文