ValidateAll controls is empty-Reset controls-Windows application
static ErrorProvider ep = new ErrorProvider();
public static bool ValidateEmptyTB(GroupBox gb1, GroupBox gb2)
{
bool ErrorinGB1 = false;
bool ErrorinGB2 = false;
ep.Clear();
bool result = false;
if (gb1 != null)
{
foreach (Control ctrl in gb1.Controls)
{
if (ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
if (txt.Text == "")
{
ErrorinGB1 = true;
ep.SetError(txt, "Empty TextBox");
}
else
{
ep.SetError(txt, "");
}
}
if (ctrl is ComboBox)
{
ComboBox cmb = (ComboBox)ctrl;
if (cmb.SelectedIndex == -1)
{
ErrorinGB1 = true;
ep.SetError(cmb, "Empty TextBox");
}
else
{
ep.SetError(cmb, "");
}
}
}
}
if (gb2 != null)
{
foreach (Control ctrl in gb2.Controls)
{
if (ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
if (txt.Text == "")
{
ErrorinGB2 = true;
ep.SetError(txt, "Empty TextBox");
}
else
{
ep.SetError(txt, "");
}
}
if (ctrl is ComboBox)
{
ComboBox cmb = (ComboBox)ctrl;
if (cmb.SelectedIndex == -1)
{
ErrorinGB2 = true;
ep.SetError(cmb, "Empty TextBox");
}
else
{
ep.SetError(cmb, "");
}
}
}
}
if (ErrorinGB1 == false && ErrorinGB2 == false)
{
ep.Dispose();
ep.Clear();
return result = true;
}
else
return result = false;
}
public static void ClearForm(Control Frm)
{
ResetFormControlValues(Frm);
}
private static void ResetFormControlValues(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.Controls.Count > 0)
{
ResetFormControlValues(c);
}
else
{
switch (c.GetType().ToString())
{
case "System.Windows.Forms.TextBox":
((TextBox)c).Text = "";
break;
//case "System.Windows.Forms.CheckBox":
// ((CheckBox)c).Checked = false;
// break;
case "System.Windows.Forms.ComboBox":
if (((ComboBox)c).Items.Count > 0)
((ComboBox)c).SelectedIndex = -1;
break;
}
}
}
}
public static bool ValidateEmptyControl(Panel pnl)
{
bool result = false;
if (pnl != null)
{
foreach (Control ctrl in pnl.Controls)
{
if (ctrl is TextBox)
{
TextBox txt = (TextBox)ctrl;
if (txt.Text.Trim() != string.Empty)
{
result = true;
}
}
if (ctrl is ComboBox)
{
ComboBox cmb = (ComboBox)ctrl;
if (cmb.Text!="--Select--")
{
result = true;
}
}
if (ctrl is RichTextBox)
{
RichTextBox rtxt = (RichTextBox)ctrl;
if (rtxt.Text.Trim()!=string.Empty)
{
result = true;
}
}
if (ctrl is DateTimePicker)
{
DateTimePicker dtp=(DateTimePicker)ctrl;
if(dtp.CustomFormat!=" ")
{
result=true;
}
}
}
}
return result;
}