Validating user input / Give .NET controls status OK or NOK

后端 未结 4 1161
鱼传尺愫
鱼传尺愫 2020-11-30 14:14

I\'m thinking about the best way to validate user input.

Let\'s imagine some TextBoxes, CheckBoxes or whatever .NET control you please, where the user input has to

4条回答
  •  庸人自扰
    2020-11-30 15:15

    It really depends how deep you want to delve into that rabbit hole...

    1. You need to decide on the validation statuses - if it's simply a case of Yes/No, then Boolean/bool will suffice, otherwise you should consider creating an enumeration to hold your validation statuses.

    2. You will need to decide whether you want to extend the controls that require validation, or just use the control's Tag property to store the validation status (personally I think that using Tag to do this is hideous).

    An Example:

    // Provides your validation statuses.
    public enum ControlValidation
    {
        Ok,
        NotOk
    }
    
    // Provides a contract whereby your controls implement a validation property, indicating their status.
    public interface IValidationControl
    {
        ControlValidation ValidationStatus { get; private set; }
    }
    
    // An example of the interface implementation...
    public class TextBox : System.Windows.Forms.TextBox, IValidationControl
    {
        public ControlValidation ValidationStatus { get; private set; }
    
        ...
    
        protected override void OnTextChanged(EventArgs e)
        {
            ValidationStatus = ControlValidation.Ok;
        }
    }
    

提交回复
热议问题