I wanted to enable/disable an Ok button on my form if some text boxes are not empty, so I tried to use Observer pattern in the following way but something sneaky is wrong with the way I am using it so Ok button always stays disabled. What can you find wrong in this code? and what are the areas for improvement?
So here is the class:
public class InputValidator
{
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 ;
}
}
}
and then here is how I am using it in my form:
private InputValidator validator;
public MyWinForm()
{
InitializeComponent();
validator = new InputValidator();
validator.RegisterTextBox(nameTextBox);
validator.ValidationDone += () => { OkButton.Enabled = true; };
}