简体   繁体   中英

Form Validation based on both Combo boxes and TextBoxes

Currently I am using a class as follows to check if yhe TextBoxes on the form that I register to it, all have a non-blank text or not and it work fine, But now I want to also add a ComboBox to this validation so that validation should be done when none of the registered textboxes AND Combobxes on the form are blank. So if I want to add a Combobx to this class, how should it look like? what is the best pracitce to do it?

public class InputValidator
{
    public delegate void ValidationDoneDelegate(bool enable);

    public event ValidationDoneDelegate ValidationDone;

    public void RegisterTextBox(TextBox tb)
    {
        tb.TextChanged += (s, e) => this.Validate(s);
    }

    private void Validate(object sender)
    {
        var t = sender as TextBox;

        if (t == null)
        {
            return;
        }

        var validationDone = ValidationDone;

        if (validationDone != null)
        {
            validationDone(!string.IsNullOrEmpty(t.Text));
        }
    }
}

I have two lists setup which will hold all the TextBox and ComboBox references. When it is time to validate, we will check all of the registered controls and if ANY of them are empty, we will be invalid. I think you will also be able to see how this can easily be extended to support additional control types.

public class InputValidator
{
    public delegate void ValidationDoneDelegate(bool enable);

    public event ValidationDoneDelegate ValidationDone;

    private List<TextBox> textBoxes = new List<TextBox>();
    private List<ComboBox> comboBoxes = new List<ComboBox>(); 

    public void RegisterTextBox(TextBox tb)
    {
        tb.TextChanged += (s, e) => this.Validate();
        textBoxes.Add(tb);
    }

    public void RegisterComboBox(ComboBox cb)
    {
        cb.SelectedValueChanged += (s, e) => this.Validate();
        comboBoxes.Add(cb);
    }

    private void Validate()
    {
        bool isValid = true;

        foreach (var tb in textBoxes)
        {
            if (string.IsNullOrEmpty(tb.Text))
                isValid = false;
        }

        if (isValid)
        {
            foreach (var cb in comboBoxes)
            {
                if (cb.SelectedItem == null)
                    isValid = false;
            }
        }

        var validationDone = ValidationDone;

        if (validationDone != null)
        {
            validationDone(isValid);
        }
    }
}

Now I'm not sure exactly what you consider to be invalid input for the ComboBox . So you may need to tweak this line to meet your needs: isValid = cb.SelectedItem != null; . I have assemed that as long as something is selected that the selection is valid.

EDIT: I had forgotten to switch the last line to validationDone(isValid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM