Container extension Properties IExtenderProvider implement WinForm Universal Data validation Component

Source: Internet
Author: User

You should be familiar with the following tip components, to let the control on the form use the ToolTip function, only need to drag a ToolTip component to the window, all the controls can use this function, to do informational hints.

What this blog is going to record is to implement a data validation component through the container extension attribute IExtenderProvider, by dragging the component into the window so that all the controls above can implement data validation!

Set the following two extended properties to use the component

After calling the open validation method public bool verifydata(Control ct = null), the validation style is:

1. Implementation ideas:

Implement prompt information for a control by documenting the validation rules for each control, and corresponding validation prompts, in conjunction with the ErrorProvider component. Time is not much, directly on the code bar, look at the note.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceCFW. winformbase.controls{/// <summary>    ///to provide data validation rules extended properties for a control/// </summary>[Description ("provide a description extended property for a menu item or control")] [Provideproperty ("Verify",typeof(Control))] [Provideproperty ("verifymsg",typeof(Control))]  Public classcontrolverify:component, IExtenderProvider {/// <summary>        ///Store the controls that are serviced and their validation rules/// </summary>        ReadOnlyDictionary<control, validata>dic; /// <summary>        ///stores the controls that are serviced and their validation hints/// </summary>        ReadOnlyDictionary<control,string>Msgdic; /// <summary>        ///Error validation Prompt class/// </summary>         PublicErrorProvider Errtip; /// <summary>        ///Create a Verify class/// </summary>         Publiccontrolverify () {dic=NewDictionary<control, validata>(); Msgdic=NewDictionary<control,string>(); Errtip=NewErrorProvider (); }        /// <summary>        ///Data Validation/// </summary>        /// <returns></returns>        /// <param name= "ct" >Verify that the control is in the container null is all</param>         Public BOOLVerifydata (Control ct =NULL)        {            //errtip.clear ();            varRET =true; foreach(varIteminchdic) {varData = Item. Key.text;//Data                varVerify = Item. Value;//validation rules                if(ct! =NULL&& item. Key.parent! =CT) {Errtip.seterror (item. Key,""); Continue; }                if(Datavali (data,verify)) {Errtip.seterror (item. Key,""); }                Else                {                    stringErrMsg =Msgdic[item.                    Key]; Errtip.seterror (item. Key, Errmsg.length>0? ErrMsg:"Please enter the correct data"); RET=false; }                            }            returnret; }        /// <summary>        ///clear the validation prompt/// </summary>         Public voidclearverify () {errtip.clear (); }        Private BOOLDatavali (stringdata,validata Vali) {            BOOLRET =false; var_data =data.            Trim (); Switch(Vali) { Casevalidata. None: Ret=true;  Break;  CaseValidata.require:if(_data. Length >0) ret=true;  Break;  CaseValidata.agevalue:if(!_data. IsNullOrEmpty () &&!_data. IsMatch ("^[0-9] + $") ) {ret=false; }                    Else{ret=true; }                     Break;  CaseValidata.DateValue:ret= _data. IsMatch (@"^ (\d{2}|\d{4}) ((0[1-9]) | ( 1[0-2]) ((0[1-9)) | ( (1|2) [0-9]) | 30|31) $");  Break;  CaseValidata.NumberValue:ret= _data. IsMatch (@"^[0-9] + $");  Break;  CaseValidata.TelValue:ret=_data.                    Isphone ();  Break;  CaseValidata.intvalue:intParse =0; RET=int. TryParse (_data, outparse);  Break;  CaseValidata.IdCardValue:ret=_data.                    Isidcard ();  Break; default:                     Break; }            returnret; }        /// <summary>        ///Get menu item Description/// </summary>[Description ("set up validation rules")]//Although the method is get, it appears as "set" in vs to understand[DefaultValue (Validata. None)] Publicvalidata getverify (Control item) {//Remove the description of the item from the collectionValidata value; stringstr; Dic. TryGetValue (item, outvalue); Msgdic.trygetvalue (item, outstr); returnvalue; }        /// <summary>        ///Set the validation rule description/// </summary>         Public voidsetverify (Control item, Validata value) {if(Item = =NULL) {return; } if(Value = =Validata. None) {//removes the item from the collection and cancels its associated event bindingDiC.                Remove (item);            Msgdic.remove (item); }            Else            {                //Add or change the description of the itemDic[item] = value;//This notation is automatically added to keys that do not exist in DICMsgdic[item] =""; }        }        /// <summary>        ///Get menu item Description/// </summary>[Description ("set up validation tips")]//Although the method is get, it appears as "set" in vs to understand[DefaultValue ("")]         Public stringgetverifymsg (Control item) {//Remove the description of the item from the collection            stringvalue; Msgdic.trygetvalue (item, outvalue); returnvalue; }        /// <summary>        ///Set Validation rule tip information/// </summary>         Public voidSetverifymsg (Control Item,stringvalue) {            if(Item = =NULL) {return; } if(Value = ="")            {                //removes the item from the collection and cancels its associated event bindingmsgdic.remove (item); }            Else            {                //Add or change the description of the itemMsgdic[item] = value;//This notation is automatically added to keys that do not exist in DIC            }        }        /// <summary>        ///whether the property can be extended for an object/// </summary>         Public BOOLCanExtend (ObjectExtendee) {            return true; }    }     Public enumValidata {None, Require, Agevalue, DateValue, Numbervalue, Telvalue, Intvalue, Idcardvalue,}}

2. Call Method:

Verify.verifydata ();

Container extension Properties IExtenderProvider implement WinForm Universal Data validation Component

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.