validate controls

D

Daniel

Hi all,

I create custom controls with textboxes on it, that need validation.

So I created methods like these...

void txtName_Validating(object sender, CancelEventArgs e)
{
// Name is required
if (txtName.Text.Trim() == "")
{
// simulate ErrorProvider
txtName.BackColor = Color.Aqua; // what about an error
message?
MessageBox.Show("Name is required!");
e.Cancel = true;
return;
}

txtName.BackColor = Color.White;
}

.... and in the Designer set the "Validating" - event to this method!

Now when I use these new custom control I want to make sure, that for
example on buttonOk-click all my controls get validated. How can I do
this?

I found examples with a Validate()-method, but I don't have this method
on compact framework!
Any workaround?

Thanks in advance
Daniel
 
G

Guest

The Validating event supposed to get triggered when you change the focus
between controls.
 
D

Daniel

ok, but I want somehow to tell a Form with my Components on it to run
through all controls and fire validation! I think I cannot do it
explicitly! Or can I do it somehow by firing events? How can I do that?

Daniel
 
G

Guest

Since Validating event is triggered when the control loses focus, you should
be able to trigger it doing something like this:

foreach (Control control in this.Controls)
{
if (control is TextBox)
{
control.Focus();
this.Focus();
}
}
 
T

tamberg

Side note: Even for a single control there seems to be a difference
between .NET and CF .NET when it comes to raising the Validated event.
The .NET CF seems not to consider clicking "ok" a focus change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top