Custom a Form Control Verification class
/// <Summary> /// Form Control Verification /// </Summary> public class winformvalidating {/// <summary> /// path verification /// </ summary> // <returns> </returns> Public static bool validtepath (control Ctrl, errorprovider EP) {string pattern = @ "^ [A-Za-Z] :( (\ (?!) [^ /:*? <> \ "" | \] +) + \\?) | (\\)?) \ S * $ "; RegEx myregex = new RegEx (pattern); bool ISR = myregex. ismatch (CTRL. Text); If (! ISR) EP. seterror (CTRL, "The entered path is invalid"); Return ISR ;} /// <summary> /// verify whether it is empty /// </Summary> /// <Param name = "Ctrl"> </param> /// <Param name = "EP"> </param> // <returns> </returns> Public static bool validatenull (control Ctrl, errorprovider EP) {string content = ctrl. text; If (content = NULL) | content. trim (). length = 0) {EP. seterror (CTRL, "cannot be blank"); Return false;} return true;} // <summary> /// Verify all controls of the form /// </Summary> /// <Param name = "f1"> </param> Public static bool validateall (Form F1) {// If a control fails verification, false bool ISR = true is returned. foreach (control CTRL in f1.controls) {ctrl. focus (); bool tempb = f1.validate (); If (! Tempb) ISR = tempb;} return ISR ;} /// <summary> /// verify the integer /// </Summary> /// <Param name = "Ctrl"> </param> /// <Param name = "EP"> </param> // <returns> </returns> Public static bool validateinteger (control Ctrl, errorprovider EP) {string context = ctrl. text; string pattern = @ "^ ([0-9] {1,}) $"; RegEx myregex = new RegEx (pattern); bool ISR = myregex. ismatch (context); If (! ISR) EP. seterror (CTRL, "the input is not a valid integer"); Return ISR ;} /// <summary> /// verify the Positive floating point number /// </Summary> /// <Param name = "? "> </Param> /// <Param name =" EP "> </param> /// <returns> </returns> Public static bool validatepositivefloat (control Ctrl, errorprovider EP) {string context = ctrl. text; string pattern = @ "^ [1-9] D *. D * | 0. D * [1-9] D * $ "; RegEx myregex = new RegEx (pattern); bool ISR = myregex. ismatch (context); If (! ISR) ep. seterror (CTRL, "the input is not a valid integer"); Return ISR ;}}
An example of the verification event in the text box to be verified is as follows:
Private void txtoutpath_validating (Object sender, canceleventargs e) {bool bt = winformvalidating. validtepath (txtoutpath, errorprovider1); If (! BT) E. Cancel = true ;}