I am trying to implement using error provider to validate that my text boxes are not empty before proceeding with execution.
Error provider works on textboxes on th
This is because your code does not check the child controls and only checks the top level ones. You need to iterate through the Form's controls recursively:
private IEnumerable GetAllControls(Control control)
{
var controls = control.Controls.Cast();
return controls.SelectMany(ctrl => GetAllControls(ctrl)).Concat(controls);
}
private void button1_Click(object sender, EventArgs e)
{
errorProvider1.Clear();
foreach (Control c in GetAllControls(this))
{
if (c is TextBox && string.IsNullOrEmpty(c.Text))
errorProvider1.SetError(c, "Error");
}
}
Or, Linq way:
errorProvider1.Clear();
GetAllControls(this).Where(c => c is TextBox && string.IsNullOrEmpty(c.Text))
.ToList()
.ForEach(c => errorProvider1.SetError(c, "Error"));
Good luck.