
Signature
Happy Coding!
Morten Wennevik [C# MVP]
Morten Wennevik [C# MVP] schrieb:
> Hi Marco,
>
[quoted text clipped - 5 lines]
> notified of an incorrect value somewhere on a form and be allowed to continue
> to enter values in other controls before correcting the error.
That's right. I can check, before Action how SAVE or UPDATE was fired,
if the UI hase invalidate controls and so I can stop the user. So I have
2 validate-methode!
1. UI (ErrorProvider)
2. BussinesObject (Methode Save with NHibernate).
The UI + BussinesObject is for the user and only BussinesObject is for a
batch insert.
Thanks
Marco
Morten Wennevik [C# MVP] - 17 Dec 2007 06:57 GMT
Hi Marco,
You didn't really answer my questions so I'm still not sure what kind of
setup you got. In any case, I have included a demonstration sample with
databinding against a business object (Person) and an ErrorProvider. I have
not used the validating event, but person.ValidateName can be used for
validation checks before saving/closing. Create a blank form called Form1
and replace the code with the following:
public partial class Form1 : Form
{
private ErrorProvider errorProvider = new ErrorProvider();
private BindingList<Person> list = new BindingList<Person>();
private TextBox textBox1 = new TextBox();
private ListBox listBox1 = new ListBox();
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
// Create some dummy data to display
list.Add(new Person("Alfa"));
list.Add(new Person("Beta"));
list.Add(new Person("Gamma"));
list.Add(new Person("All"));
// Databindings
listBox1.DataSource = list;
listBox1.DisplayMember = "Name";
textBox1.DataBindings.Add("Text", list, "Name");
// We'll update the errorprovider when the text is changed
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
// Add the controls
Controls.Add(textBox1);
listBox1.Location = new Point(0, 30);
Controls.Add(listBox1);
}
void textBox1_TextChanged(object sender, EventArgs e)
{
// Get the selected person and display any error text it contains
Person person = listBox1.SelectedItem as Person;
if(person != null)
errorProvider.SetError(sender as Control, person.NameError);
}
// Our business object
class Person
{
private string _name;
private string _nameError;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
// Any changes to the Name property
// will call the ValidateName method
ValidateName();
}
}
}
// This property contains any error text for this business object
public string NameError
{
get { return _nameError; }
set
{
if (_nameError != value)
{
_nameError = value;
}
}
}
public Person(string name)
{
this.Name = name;
}
// This method has a double function
// When called it will update NameError with the proper error text
// but it will also let the caller know if the business object
is valid
// 'if(person.ValidateName()){ SavePerson(person); }'
public bool ValidateName()
{
if (!Name.StartsWith("A"))
{
NameError = "Name must start with A";
return false;
}
else
{
NameError = "";
return true;
}
}
}
}

Signature
Happy Coding!
Morten Wennevik [C# MVP]
> Morten Wennevik [C# MVP] schrieb:
> > Hi Marco,
[quoted text clipped - 19 lines]
> Thanks
> Marco
Marco Schwarz - 18 Dec 2007 13:10 GMT
Sorry now I unterstand answer, thanks
Morten Wennevik [C# MVP] schrieb:
> Hi Marco,
>
[quoted text clipped - 106 lines]
> }
> }