DevExpress controls extension methods to achieve parity

Rules need to be verified for each control to be verified when DevExpress controls, if you want to verify the value of the control, the need to use DXValidationProvider DXErrorProvider controls and controls, in accordance with normal thinking, regardless of which control uses to achieve parity effect binding, in this, write an extension method to achieve the effect of once and for all.

Spreading code as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using DevExpress.XtraEditors.DXErrorProvider;
    using System.Windows.Forms;
     
    namespace ValidateForm
    {
    ///<summary>
    ///验证值是否存在的验证规则
    ///</summary>
    public class CustomValidationRule : ValidationRule
        {
    public CustomValidationRule(Func<object, bool>validationFunction)
            { 
    The this .ValidateFunction = validationFunction; 
            } 
     
    /// <Summary> 
    /// Gets or sets the authentication method
     /// </ Summary> 
    Func < Object , BOOL > ValidateFunction { GET ; SET ;} 
     
    /// <Summary> 
    // / verify control values to be authenticated exists
     /// </ Summary> 
    /// <param name = "control"> be verified control. </ param> 
    /// <param name = "value"> values to be verified control </ param> 
    /// <Returns> If the value already exists, it returns false; true otherwise. <
       Validate(System.Windows.Forms.Control control, object value)
            {
    if (this.ValidateFunction == null)
    throw new InvalidOperationException("必须设置ValidateFunction属性");
    return ValidateFunction(value);
            }
        }
     
    public static class DXValidationExtension
        {
     
    static Dictionary<int, Dictionary<int, List<DXValidationProvider>>>providerDictionary;
     
    staticDXValidationExtension () 
            { 
    providerDictionary = newDictionary < int , the Dictionary < int , List <DXValidationProvider >>> (); 
            } 
     
    /// <Summary> 
    /// to control container to be verified to verify control, and returns the verification result.
    /// </ Summary> 
    /// <param name = "Container"> control container </ param> 
    /// <Returns> </ Returns> 
    public  static  BOOL DXValidate ( the this Control Container) 
            { 
    BOOL isValid = to true ; / / validation results 
     
    foreach 
                { 
    the foreach ( var Provider indict.Value) 
                    { 
    IF (! provider.Validate ()) 
                        { 
    // either not verified, then the verification result is set to false 
    isValid = to false ;
     // be verified control any of a rule does not pass validation, an immediate exit, no other rules to verify the control 
    BREAK ; 
                        } 
                    } 
                } 
    return isValid; 
            } 
     
    public  static  void CustomValidation ( the this control Container, control ControlToValidate, String errorText, 
    Func<object, bool> validate)
            {
    CustomValidation(container, controlToValidate, errorText, validate, false, true);
            }
     
    private static void CustomValidation(this Control container, Control controlToValidate, string errorText, Func<object, bool> validate, bool alignRight, bool manualValidation)
            {
    //获得带验证的控件的DXValidationProvider集合。
    var dictionary = GetProviderDictionary(container);
    var providers = GetProvider(controlToValidate, dictionary);
    var provider = new DXValidationProvider();
    provider.ValidationMode = manualValidation ? ValidationMode.Manual : ValidationMode.Auto;
    provider.SetIconAlignment(controlToValidate, alignRight ? ErrorIconAlignment.MiddleRight : ErrorIconAlignment.MiddleLeft);
     
    provider.SetValidationRule(controlToValidate, newCustomValidationRule(validate)
                {
    ErrorText = errorText,
                });
     
    //Adding to the collection DXValidationProvider DXValidationProvider be verified control. 
    providers.Add (Provider); 
            } 
     
    /// <Summary> 
    /// Returns validation control DXValidationProvider be set
     /// </ Summary> 
    /// <param name = "ControlToValidate"> be authenticated control </ param> 
    / // <param name = "the Dictionary"> pending validation controls where the dictionary table </ param> 
    /// <returns a> </ returns a> 
    Private  static List <DXValidationProvider> GetProvider (control ControlToValidate, the Dictionary < int , List <DXValidationProvider> >
    if (dictionary.ContainsKey(key))
    return dictionary[key];
    else
                {
    var providers = newList<DXValidationProvider>();
    dictionary.Add(key, providers);
    return providers;
                }
            }
     
    ///<summary>
    ///返回空间容器的待验证控件字典
    ///</summary>
    ///<param name="container"></param>
    ///<returns></returns>
    private static Dictionary<int, List<DXValidationProvider>>GetProviderDictionary(Control container)
            {
    var key = container.GetHashCode();
    if (providerDictionary.ContainsKey(key))
                {
    return   providerDictionary[key];
                }
    else
                {
    var providers = newDictionary<int, List<DXValidationProvider>>();
    providerDictionary.Add(key, providers);
    return providers;
                }
            }
        }
    }

 

Program calling code examples are as follows:

    using System;
     
    using System.Collections.Generic;
     
    using System.ComponentModel;
     
    using System.Data;
     
    using System.Drawing;
     
    using System.Linq;
     
    using System.Text;
     
    using System.Threading.Tasks;
     
    using System.Windows.Forms;
     
    namespace ValidateForm
     
    {
     
    public partial class Form1 : Form
     
        {
     
    public Form1()
     
            {
     
    InitializeComponent();
     
    InitValidateRoles();
     
            }
     
    private void InitValidateRoles()
     
            {
     
    this.CustomValidation(calcEdit1, "请输入大于0小于20的值", value =>
     
                {
     
    returnConvert.ToInt32(calcEdit1.EditValue) >  &&Convert.ToInt32(calcEdit1.EditValue) <;
     
                });
     
            }
     
    private void simpleButton1_Click(object sender, EventArgs e)
     
            {
     
    if (!this.DXValidate())
     
    return;     
     
            }
     
        }
     
    }

 

Guess you like

Origin www.cnblogs.com/MuNet/p/11487878.html