快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

吉祥体育大厅app:Winform自定义控件,信用卡、银行卡输入控件;每4个字符分隔显示



常常在网银买卖营业的时刻必要输入卡号,看到输入的卡号都被每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吉祥体育大厅app;

namespace BarCodeCommon {

/吉祥体育大厅app///// 信用卡、银行卡,卡号输入控件;也可以用来输入手机号,4个字符分隔一下。

///public partial class BankCardNoInputTextBox : TextBox

{public override string Text

{get

{return base.Text.WordStr(" ","");

}set

{base.Text = value;

}}

public BankCardNoInpu吉祥体育大厅apptTextBox()

{InitializeComponent();

Font = new 吉祥体育大厅appFont("幼圆", 10F, FontStyle.Bold);ForeColor = Color.DarkBlue;

}

protected override void OnTextChanged(EventArgs e){

if (SelectionStart > 4){

string result = string.Empty;var tmpTxt = this.Text.WordStr(" ", "").ToArray();

int cnt = Convert.ToInt32(Math.Ceiling((double)tmpTxt.Length / 4));for (int i = 0; i

{var fourChar =吉祥体育大厅app 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 + xif (c.Equals((char)3) || c.Equals((char)22) || c.Equals((char)24))

{return;

}//判断是否是数字

bool blA = !(c >= 48 && c//判断是否跨越小数点的位数,判断是否会呈现数据类型冲突

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;

}}

} }

您可能还会对下面的文章感兴趣: