資源簡介
自定義的一個TextBox控件,包含限制只能輸入數(shù)字、驗證郵箱、電話、手機、身份證號碼、郵件地址等功能,提供源碼,蠻適合初學者學習和使用的~~

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Text.Regularexpressions;
using?TextBoxM;
namespace?TextBoxM
{
????//創(chuàng)建一個枚舉類
????public?enum?AllowType
????{
????????All
????????Num
????????UserName
????????Psw
????????Email
????????IDcard
????????MobliePhone
????????Tel
????}
????
????public?class?AgiaTxetBox:TextBox
????{
????????
????????private?AllowType?_allowType?=?AllowType.All;
????????public?AllowType?Type
????????{
????????????get?{?return?_allowType;?}
????????????set?{?_allowType?=?value;?}
????????}
????????//string?UserName?=?“^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$“;//限制字母、數(shù)字和下劃線,下劃線不能放在最末
????????
????????
????????///?
????????///?身份證驗證方法
????????///?
????????///?
????????///?
????????public?static?bool?IsValidIdcard(string?Id)
????????{
????????????if?(Id.Length?!=?18)
????????????{
????????????????return?false;
????????????}
????????????//數(shù)字驗證
????????????long?n?=?0;
????????????if?(long.TryParse(Id.Remove(17)?out?n)?==?false?||?n?????????????{
????????????????return?false;
????????????}
????????????//省份驗證?
????????????string?address?=?“11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91“;
????????????if?(address.IndexOf(Id.Remove(2))?==?-1)
????????????{
????????????????return?false;
????????????}
????????????//生日驗證
????????????string?birth?=?Id.Substring(6?8).Insert(6?“-“).Insert(4?“-“);
????????????DateTime?time?=?new?DateTime();
????????????if?(DateTime.TryParse(birth?out?time)?==?false)
????????????{
????????????????return?false;
????????????}
????????????//校驗碼驗證
????????????string[]?arrVarifyCode?=?(“10x98765432“).Split(‘‘);
????????????string[]?Wi?=?(“7910584216379105842“).Split(‘‘);
????????????char[]?Ai?=?Id.Remove(17).ToCharArray();
????????????int?sum?=?0;
????????????for?(int?i?=?0;?i?17;?i++)
????????????{
????????????????sum?+=?int.Parse(Wi[i])?*?int.Parse(Ai[i].ToString());
????????????}
????????????int?y?=?-1;
????????????Math.DivRem(sum?11?out?y);
????????????if?(arrVarifyCode[y]?!=?Id.Substring(17?1).ToLower())
????????????{
????????????????return?false;
????????????}
????????????return?true;
????????}
???????
????????
????????///?
????????///?重寫KeyPress事件
????????///?
????????///?
????????protected?override?void?onkeypress(KeyPressEventArgs?e)
????????{
????????????e.Handled?=?true;
????????????if?(_allowType?==?AllowType.Email?||?_allowType?==?AllowType.UserName?||?_allowType?==?AllowType.All?||?_allowType?==?AllowType.Psw)
????????????{
????????????????e.Handled?=?false;
????????????}
????????????//限制只能輸入數(shù)字
????????????if
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????920??2011-11-25?14:33??TextBoxW.sln
????..A..H.?????12800??2011-11-25?14:37??TextBoxW.suo
?????文件???????2597??2011-11-25?14:32??TextBoxM\AgiaTextBox.csproj
?????文件???????7577??2011-11-25?14:32??TextBoxM\AgiaTxetBox.cs
?????文件??????15872??2011-11-25?14:33??TextBoxM\bin\Debug\TextBoxM.pdb
?????文件????????373??2011-11-25?14:33??TextBoxM\obj\Debug\AgiaTextBox.csproj.FileListAbsolute.txt
?????文件????????951??2011-11-25?14:32??TextBoxM\obj\Debug\TextBoxM.csproj.FileListAbsolute.txt
?????文件???????7680??2011-11-25?14:33??TextBoxM\obj\Debug\TextBoxM.dll
?????文件??????15872??2011-11-25?14:33??TextBoxM\obj\Debug\TextBoxM.pdb
?????文件???????1348??2011-11-20?11:19??TextBoxM\Properties\AssemblyInfo.cs
?????目錄??????????0??2011-11-25?14:32??TextBoxM\obj\Debug\Refactor
?????目錄??????????0??2011-11-20?11:19??TextBoxM\obj\Debug\TempPE
?????目錄??????????0??2011-11-25?14:33??TextBoxM\bin\Debug
?????目錄??????????0??2011-11-25?14:33??TextBoxM\obj\Debug
?????目錄??????????0??2011-11-25?14:32??TextBoxM\bin
?????目錄??????????0??2011-11-20?11:19??TextBoxM\obj
?????目錄??????????0??2011-11-20?11:19??TextBoxM\Properties
?????目錄??????????0??2011-11-25?14:33??TextBoxM
-----------?---------??----------?-----??----
????????????????65990????????????????????18
評論
共有 條評論