[C#] Auto fill data from clipboard to list TextBox

[C#] Auto fill data from clipboard to list TextBox , [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách auto fill dữ liệu từ clipboard windows vào danh sách Textbox trên C#, winform.

[C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform


Dưới đây, là hình ảnh demo ứng dụng:

Như hình trên, các bạn thấy ở trường dữ liệu nhập số căn cước công dân (CCCD).

Nó bao gồm 12 control Textbox như hình trên.

Khi chúng ta copy một dãy dữ liệu và kiểm tra nó đúng 12 số và không tồn tại ký tự đúng format của số căn cước công dân, thì chúng ta sẽ điền chúng vào tất cả Textbox trên.

Video demo ứng dụng:


Full source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace FillFormClipboard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            foreach (Control control in panel1.Controls)
            {
                if (control is TextBox)
                {                   
                    TextBox textBox = (TextBox)control;
                    textBox.KeyDown += TextBox_KeyDown;
                    textBox.KeyPress += TextBox_KeyPress;
                }
            }
        }

        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            // chỉ cho phép nhập số
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {                
                e.Handled = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
           
            if (e.Control && e.KeyCode == Keys.V)
            {               
                HandlePasteEvent((TextBox)sender);
            }
        }

        private void HandlePasteEvent(TextBox textBox)
        {
           
            if (Clipboard.ContainsText())
            {
                string clipboardText = Clipboard.GetText().Trim().Replace(" ","");
                var regex = new Regex(@"^\d{12}$");
                if (clipboardText.Length == 12 && regex.IsMatch(clipboardText))
                {
                    char[] characters = clipboardText.ToCharArray();

                    foreach (Control control in panel1.Controls)
                    {
                        if (control is TextBox)
                        {
                            var nameControl = control.Name;
                            var i = Convert.ToInt16(nameControl.Split('_').LastOrDefault()) - 1;
                            control.Text = characters[i].ToString();
                        }
                    }
                }
            }
        }

    }
}

Chúc các bạn thành công với thủ thuật trên nhé.

DOWNLOAD SOURCE CODE

PASSWORD UNZIP: HUNG.PRO.VN 

2 comments

  1. thủ thuật này hay đó e (y) - có thể tích hợp vào nhập key bản quyền, để nó tự động các ô chứ mình không mất công nhập từng ô đó e <3
    1. hehe, tích hợp vào thì được a, nhưng để làm chương trình bản quyền thế nào mới là điều khó đó a :D
MỌI NGƯỜI GÓP Ý CHO HUNG.PRO.VN NHÉ.
ĐỂ XEM NHẬN XÉT MỚI NHẤT /comment
ĐỂ XEM BÀI VIẾT MỚI NHẤT /sitemap