0

Let's say I have a form with some text boxes and combo boxes on it and some of these are required controls to be filled out before we let the OK button on the form to get enabled.

So one way is to have a centralized method like EnableOK() method that checks if those required combobxes and text boxes are filled out or not...and then I have to go on TextChanged or IndexChanged events of those required controls call this EnableOK() method.

So I was thinking if there is better way of doing this? maybe there is a pattern we can use? so that I won't have to call EnableOk() in every place,or maybe there is not a better way and that's the way every body else is doing it?

P.S: I am using C# Winforms.

3 Answers 3

2

One approach is not to disable the OK button at all. Instead, when the user presses OK, do your check and present a MessageBox if something is wrong.

I like this approach for several reasons.

  1. The user can never end up wondering why the OK button isn't enabled.
  2. You can tell the user exactly why it is not OK to proceed. You can tell them where they "messed up".

I personally prefer this method. It shouldn't be up to the user to figure out what is considered acceptable input. I prefer to simply nudge the user by telling them where they need to modify their input when it is wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

well that's in the requirements of my project. So I can't do this.
In cases where this is not possible due to project requirements, I have found myself performing validation in exactly the way you outlined in your question. One small change you can use is only do the check upon the commit event of the fields. So that (if you have a complicated validation) you aren't doing it on every keystroke.
1

Create an observer, wich will subscribe to all textBox.TextChanged events.

public class IputValidator
{ 
     public event Action ValidationDone;
     private List<TextBox> boxes = new List<TextBox>();
     public void RegisterTextBox(TextBox tb) 
     { 
       tb.TextChanged += (s,e) => Validate;
       boxes.Add(tb);
     }
     public void Validate() 
     { 
        foreach(var t in boxes)
        {
          if(string.IsNullOrEmpty(t.Text)) return;
        }
        //all data inputed. fire validationDone event.
     }
}

It will wait while all necessary data will be inputed. Then, it will enable Ok button.


using

public partial class YourForm : Form 

{
  private InputValidator _validator;

  public YourForm() 
  {
     InitializeComponents(); //I don't remember the right name in WinForms
     _validator = new InputValidator();
     _validator.RegisterTextBox(_textBox1);
     _validator.ValidatonDone += () => { _okButton.Enable = true;} 
  }

}

4 Comments

Thanks, Just a dumb question: Now how do I use it on my form? should my form inherit from this? or should it create an instance of it?
You can use it as you want ;) Create an instance in constructor, save it as a class variable, and register all textboxes in it, for example.
Thanks for the update...tried to use it but didn't work, so a question: We have registered these in Constructor of the form, so at the beginning the text boxes are empty so it will return false, button disalbed...but when we start typing in the text boxes, how should know and fire the event that something has changed in the text box so go and check validate it again?
tb.TextChanged += (s,e) => Validate; <--- when this event fires, it will check all textboxes again.
1

Here is architecture approach that enables you some complex validation on control side:

  1. create an interface that will be common for all control types something like:
interface iMyControls
{
    bool ValidateControl();
}
  1. create your own classes that inherits from required controls and implements iMyControls:

    internal class MyTextBox : System.Windows.Forms.TextBox, iMyControls
    {
         public bool ValidateControl()
         {
             return this.Text != "";
         }
    }
    
  2. on your form just go over this controls and call:

    bool isValid = myControl.ValidateControl();
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.