By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

Windows.Forms validation strategy

P: n/a
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?
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Kathy" <katnel20 at hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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?

Nov 17 '05 #2

P: n/a
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.

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:ht********************@giganews.com...
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
"Kathy" <katnel20 at hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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?


Nov 17 '05 #3

P: n/a
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

"Kathy" <katnel20 at hotmail.com> wrote in message
news:OU**************@tk2msftngp13.phx.gbl...
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.

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:ht********************@giganews.com...
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
"Kathy" <katnel20 at hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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?



Nov 17 '05 #4

P: n/a
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.

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:6e******************************@giganews.com ...
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

"Kathy" <katnel20 at hotmail.com> wrote in message
news:OU**************@tk2msftngp13.phx.gbl...
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.

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:ht********************@giganews.com...
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
"Kathy" <katnel20 at hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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?



Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.