Hi...
The below event handlers validates the text boxes and comboboxes in windows forms application.
First drag and drop "ErrorProvider" control from Components section of Toolbox onto your form and give its Id "errorProvider1". Then just copy below code block in your code file. Then in "Validating" event of text box select "TextBox_Validating" event handler and in "Validating" event of combobox select "ComboBox_Validating" event.
Set form's "AutoValidate" property to "Disable".
private void TextBox_Validating(object sender, CancelEventArgs e)
{
TextBox objTextBox = (TextBox)sender;
if (objTextBox.Text.Trim() == string.Empty)
{
errorProvider1.SetError(objTextBox, "Required");
e.Cancel = true;
}
else
{
errorProvider1.SetError(objTextBox, null);
}
}
private void ComboBox_Validating(object sender, CancelEventArgs e)
{
ComboBox objComboBox = (ComboBox)sender;
if (objComboBox.SelectedIndex <= 0)
{
errorProvider1.SetError(objComboBox, "Select");
e.Cancel = true;
}
else
{
errorProvider1.SetError(objComboBox, null);
}
}
Then on button click event, you need to check this:
if (this.ValidateChildren() == true)
else
return;
Its done now. :)