Bohn
Bohn

Reputation: 26929

Enable logic for a button on a form with required controls

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.

Upvotes: 0

Views: 1281

Answers (3)

Anton Sizikov
Anton Sizikov

Reputation: 9250

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;} 
  }

}

Upvotes: 1

Gregor Primar
Gregor Primar

Reputation: 6805

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();
    

Upvotes: 1

Michael Mankus
Michael Mankus

Reputation: 4788

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.

Upvotes: 2

Related Questions