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

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 

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

2 comments:

Ngọc Ánh said...

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

Hung Program VN said...

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

All Right Reserved © 2015 By Hung Pro VN