If you're talking about data validation, you can use
ErrorProvider Class (System.Windows.Forms) | Microsoft Docs[
^]
For further details, please see:
How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component | Microsoft Docs[
^]
Note: if there's a set of different rules for each validation, i'd create a dictionary of ErrorProviders for each control you want to validate. For example, if you need to check if entered value is a number, you need to create a "NumberErrorProvider", if you want to check if phone no. is valid, create "PhoneNoErrorProvider", etc...
Finally, you can create custom method (for example:
IsFormValid
) which steps through the collection ErrorProviders and returns true/false.
public partial class Form1 : Form
{
private Dictionary<Control, ErrorProvider> validators = new Dictionary<Control, ErrorProvider>();
public Form1()
{
InitializeComponent();
validators.Add(textBox1, new ErrorProvider(this){
validators.Add(comboBox1, new ErrorProvider(this){
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (IsValidNumber(textBox1.Text))
validators[textBox1].SetError(textBox1, "Field 'Number' has to be a valid number!");
else
validators[textBox1].Clear();
}
private bool IsFormValid()
{
foreach (Control c in validators.Keys)
{
if(validators[c].GetError(c) !="")
return false;
}
return true;
}
}
This is a basic concept for WinForms data validatation. You can implement your own.
Good luck!