1. create the form.
2. create the Business-object Customer, implement the IDataErrorInfo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WindowsFormsApplication_error
{
public class Customer : IDataErrorInfo
{
public Customer() { }
private string _firstName;
//[Bindable(true)]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
//[Bindable(true)]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _street;
//[Bindable(true)]
public string Street
{
get { return _street; }
set { _street = value; }
}
#region IDataErrorInfo Members
public string Error
{
get
{
StringBuilder sb = new StringBuilder();
sb.Append(this["FirstName"]);
sb.Append(this["LastName"]);
return sb.ToString();
}
}
public string this[string columnName]
{
get
{
if (columnName == "FirstName")
{
if (String.IsNullOrEmpty(FirstName))
return "Type required.";
}
else if (columnName == "LastName")
{
if (String.IsNullOrEmpty(LastName))
return "Symbol required.";
}
return string.Empty;
}
}
#endregion
// used for check the form is valid or not (before save or do something else)
public bool IsValid()
{
if (Error == string.Empty)
return true;
else
return false;
}
}
}
3, Add data source.
Choose Data -> add new data source -> Object. Then select the Customer class for binding.
4,Bind the controls.
select Data -> show DataSources, in the left part of ide, drag and drop the bound controls to the form.
5. Add an errorProvide-control, Drag an ErrorProvider to the WinForm and set the Datasource to the CustomerBindingSource in the Properties-Sheet. PropertyAutoValidate allow/prevent the focus changes when error happens.
6. Add a customer object as datasource
public Main()
{
InitializeComponent();
customerBindingSource.DataSource = new Customer();
}
7. change the validation type (when the validation fire)
select the textbox which need validating in design window, go to property window, click the DataBindings - > advanced. change the Data source update mode.
// real example
class AccountTransactionObj : IDataErrorInfo
{
[Bindable(true)]
public decimal Amount { get; set; }
[Bindable(true)]
public string Description{ get; set; }
#region IDataErrorInfo Members
public string Error
{
get
{
StringBuilder sb = new StringBuilder();
sb.Append(this["Amount"]);
sb.Append(this["Description"]);
return sb.ToString();
}
}
public string this[string columnName]
{
get
{
if (columnName == "Amount")
{
if (Amount <= 0)
return "Value must be greater than 0";
}
else if (columnName == "Description")
{
if (String.IsNullOrEmpty(Description))
{
return "Description cannot be empty";
}
}
return string.Empty;
}
}
public bool IsValid
{
get
{
if (Error == string.Empty)
return true;
else
return false;
}
}
#endregion
}
// how to use
public partial class AccountTransactionForm : RiteweighFormBase
{
#region properties
private Account Account { get; set; }
private AccountTransactionObj AccountTransactionObj { get; set; }
#endregion
#region Construction
/// <summary>
/// Constructor
/// </summary>
public AccountTransactionForm(Account account)
: base()
{
InitializeComponent();
Account = account;
DataBind();
}
#endregion
#region Databinding
private void DataBind()
{
AccountTransactionObj = new AccountTransactionObj();
bindingSource1.DataSource = AccountTransactionObj;
bindingSource1.CurrentItemChanged += new EventHandler(bindingSource1_DataSourceChanged);
TextBoxAmount.DataBindings.Add(new Binding("Text", bindingSource1, "Amount", true, DataSourceUpdateMode.OnPropertyChanged));
TextBoxDescription.DataBindings.Add(new Binding("Text", bindingSource1, "Description", true, DataSourceUpdateMode.OnPropertyChanged));
UpdateUI();
}
void bindingSource1_DataSourceChanged(object sender, EventArgs e)
{
UpdateUI();
}
private void UpdateUI()
{
ButtonOk.Enabled = IsValid;
}
#endregion
#region Event Handling
private void ButtonOk_Click(object sender, EventArgs e)
{
if (IsValid)
{
// do something
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
//do something
}
#endregion
private bool IsValid
{
get { return AccountTransactionObj.IsValid; }
}
}