NEWS

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

[C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
Đăng bởi: Thảo Meo - Lượt xem: 1776 08:37:07, 06/12/2023C#   In bài viết

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#] Auto fill data from clipboard to list TextBox

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

thumb_auto_fill

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

    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
Đăng bởi: Thảo Meo - Lượt xem: 1776 08:37:07, 06/12/2023C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.