Windows.Forms validation strategy

K

Kathy

I have a form to generate a report.
On the form are several textboxes and a "Generate" button.
I would like to have the button grayed (not enabled), unless the data in
each of the dependant textboxes is valid.
What is the proper procedure for doing this?

It seems the Validating and Validated events for each control only fire when
it loses focus.
I would like the button to be enabled as soon as the data is valid (before
leaving the control).
Also, how do I "sum up" all the controls into one master enable?
 
P

Pete Davis

Well, the .NET validation strategy is to validate each control as it loses
focus. The reason for this is that in most cases, for a text box, for
example, you don't want to validate in until the user is done typing. Their
partial text is likely to be invalid if it's supposed to meet some sort of
validation requirement, so you don't want there to be an error until after
it loses focus.

In your case, you want to validate as they type and it sounds like you want
to validate all the text boxes.

So, it's fairly simple. For each textbox, add an event handler for
TextChanged. You only need a single method that actually handles it for all
the textboxes, so for each textbox, you'd add that one. For example:

textBox1.TextChanged += new EventHandler(ValidateTextBoxes);
textBox2.TextChanged += new EventHandler(ValidateTextBoxes);
textBox3.TextChanged += new EventHandler(ValidateTextBoxes);

private void ValidateTextBoxes(object sender, EventArgs e)
{
... validate all my textboxes here ...
}


When they're in a valid state, simply enable the button.

That should work. If you need to know which textbox sent the message, simply
cast "sender" to a TextBox

Pete
 
K

Kathy

Ok Pete, I'll buy that.
I guess seeing the 2 Validation events and other methods in the control, I
assumed the Framework authors had some sort of "grand scheme" that I was
overlooking.

On a related topic, Java has the concept of a Document model that can be
attached to a textbox which monitors the TextChanged event and limits input
to valid chars. Is there something similar in .NET?

Kathy.
 
P

Pete Davis

Kathy,

Not so much a "document model". You can bind datasets (and datatables and
such) to controls on your form. For limiting input to valid characters, you
pretty much have to do that on your own, if I'm understanding you.

I mean, generally, you'd want to create custom controls that handle
formatting of text, or at least that's what I'd do. For example, if I needed
a control that handled only numeric data (and I planned on re-using it in
the app), I'd derive a class from TextBox that restricted the input. That
kind of stuff is fairly trivial to do.

But then my job is creating custom .NET controls, so I have a tendency
to handle things in that way.

Did you have something else in mind?

Pete
 
K

Kathy

No, I just thought there would be a class in the framework already so I
didn't have to do the "char at a time" validation.
I did a lot of forms in Java and got use to that framework. I just figured
Microsoft would have done the same or at least gone one better.

Thanks for the help.
Kathy.
 

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