zoukankan      html  css  js  c++  java
  • 贡献个Winform自定义控件,信用卡、银行卡输入控件;每4个字符分隔显示。其他UI框架可以参考。

    经常在网银交易的时候需要输入卡号,看到输入的卡号都被每4个字符分隔显示,这样很方便操作者核对。最近在弄WinForm的项目,赶紧也实现一个。要的快来!

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BarCodeCommon
    {
        /// <summary>
        /// 信用卡、银行卡,卡号输入控件;也可以用来输入手机号,4个字符分隔一下。
        /// </summary>
        public partial class BankCardNoInputTextBox : TextBox
        {
            public override string Text
            {
                get
                {
                    return base.Text.Replace(" ","");
                }
                set
                {
                    base.Text = value;
                }
            }
    
            public BankCardNoInputTextBox()
            {
                InitializeComponent();
                Font = new Font("幼圆", 10F, FontStyle.Bold);
                ForeColor = Color.DarkBlue;
            }
    
            protected override void OnTextChanged(EventArgs e)
            {                        
                if (SelectionStart > 4)
                {
                    string result = string.Empty;
                    var tmpTxt = this.Text.Replace(" ", "").ToArray();
                    int cnt = Convert.ToInt32(Math.Ceiling((double)tmpTxt.Length / 4));
                    for (int i = 0; i < cnt; i++)
                    {
                        var fourChar = tmpTxt.Skip(i * 4).Take(4);
                        result += string.Join("", fourChar) + " ";
                    }
                    Text = result;
                    SelectionStart = result.Length-1;
                }
            }
    
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                char c = e.KeyChar;
                //如果是ctrl + c 、ctrl + v、ctrl + x
                if (c.Equals((char)3) || c.Equals((char)22) || c.Equals((char)24))
                {
                    return;
                }
                //判断是否是数字
                bool blA = !(c >= 48 && c <= 57) && c != 8 && c != 43 && c != 45 && c != 46;
                //判断是否超过小数点的位数,判断是否会出现数据类型冲突
                bool blC = (c == 43 || c == 45) && (this.Text.IndexOf("+") >= 0 || this.Text.IndexOf("-") >= 0)
                           && this.SelectedText != "+" && this.SelectedText != "-";
                if (blA || blC)
                {
                    e.Handled = true;
                    return;
                }
            }
        }
    }
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    "作者:" 数据酷软件工作室
    "出处:" http://datacool.cnblogs.com
    "专注于CMS(综合赋码系统),MES,WCS(智能仓储设备控制系统),WMS,商超,桑拿、餐饮、客房、足浴等行业收银系统的开发,15年+从业经验。因为专业,所以出色。"
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • 相关阅读:
    HDOJ 1846 Brave Game
    并查集模板
    HDU 2102 A计划
    POJ 1426 Find The Multiple
    POJ 3278 Catch That Cow
    POJ 1321 棋盘问题
    CF 999 C.Alphabetic Removals
    CF 999 B. Reversing Encryption
    string的基础用法
    51nod 1267 4个数和为0
  • 原文地址:https://www.cnblogs.com/datacool/p/BankCardNoInputTextBox_Winform.html
Copyright © 2011-2022 走看看