15 March 2014

ComboBox With Error Provider in C#.net

ComboBox With Error Provider in C#.net


Create CustomError Provider in your project
 using System.Collections.Generic;  
using System.Windows.Forms;
namespace naraayananProject.UserControl
{
public class CustomErrorProvider :ErrorProvider
{
public List<Control> GetControls()
{
return this.GetControls(this.ContainerControl);
}
public List<Control> GetControls(Control ParentControl)
{
List<Control> ret = new List<Control>();
if (!string.IsNullOrEmpty(this.GetError(ParentControl)))
ret.Add(ParentControl);
foreach (Control c in ParentControl.Controls)
{
List<Control> child = GetControls(c);
if (child.Count > 0)
ret.AddRange(child);
}
return ret;
}
}
}

Declare Custom Error Provider in your Current Class:
 public static CustomErrorProvider ErrProvider = new CustomErrorProvider();  

  public static bool ComboBoxErrorControl(ComboBox cmb, string message, TabPage tp)  
{
if (!String.IsNullOrEmpty(cmb.Text))
{
ErrProvider.SetError(cmb, "");
}
else
{
ErrProvider.SetError(cmb, message);
}
object[] control = ErrProvider.GetControls(tp).ToArray();
if (control.Length > 0)
return false;
else
return true;
}

No comments: