473,788 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows.Forms validation strategy

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
4 2676
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.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox2.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox3.TextCh anged += new EventHandler(Va lidateTextBoxes );

private void ValidateTextBox es(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******** ********@TK2MSF TNGP10.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
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******** ************@gi ganews.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.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox2.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox3.TextCh anged += new EventHandler(Va lidateTextBoxes );

private void ValidateTextBox es(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******** ********@TK2MSF TNGP10.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
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******** ******@tk2msftn gp13.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******** ************@gi ganews.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.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox2.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox3.TextCh anged += new EventHandler(Va lidateTextBoxes );

private void ValidateTextBox es(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******** ********@TK2MSF TNGP10.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
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******** *************** *******@giganew s.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******** ******@tk2msftn gp13.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******** ************@gi ganews.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.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox2.TextCh anged += new EventHandler(Va lidateTextBoxes );
textBox3.TextCh anged += new EventHandler(Va lidateTextBoxes );

private void ValidateTextBox es(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******** ********@TK2MSF TNGP10.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2314
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the example source code can be downloaded here: http://www.blogitek.com/timhaughton/archives/files/User%20Interrogator%20And%20TDD.zip The article text is below. Not sure what it will do to the formatting when
0
2413
by: Frans Bouma | last post by:
Hello, It seems VS.NET 2003 locks up itself and the complete shell (mouse locks also) when entering a breakpoint in a special situation. Below is the code to reproduce this behavior. It contains a line where you have to set a breakpoint (the first line in the validation event handler). Compile and run in the debugger in vs.net using F5. When you hit 'enter' in the textbox, the debugger should break in the validator routine where you...
5
73216
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
0
1024
by: honcho | last post by:
My ASP.NET web application has a flaw that produces false security-violation alarms. There are several categories of users for this web site, e.g. Colonels, Sergeants, Private_1s, and Private_2s. Colonels may visit any page in the web site. Sergeants may visit only non-com pages. The non-com pages consist of non-com-0, non-com-1, and non-com-2 pages. Private_1s may visit non-com-0 and non-com-1 pages. Private_2s may visit...
3
1299
by: Mr Newbie | last post by:
I am using a DataGrid for Edit and Update. However, Im wondering what the best strategy for client side validation would be as I dont think the validators can reference the TextBoxes which come up in the Table Rendered by the DataGrid when Editing/Updating or maybe I'm wrong ????? I know I can validate inside the update event, but i wondered if anyone has tried to do this client side ? Cheers - Mr Newbie.
12
2050
by: Dabbler | last post by:
I need to insure that at least one of three phone number fields has a value (requiredfield) but I'm not sure of a way to implement this without server side logic. Is there a way to use the validation controls to do this? Thanks.
16
20689
by: Daniela Roman | last post by:
Hello, can someone tell me the main differences between the web and windows based applications? thank you
3
2765
by: Yohan Blurp | last post by:
Hello everybody. I'm writing a Perl script managing forms submissions but encounter some difficulty on a step. 1) User submits a form (using POST method). The page being declared as cacheable with "Cache-Control: max-age=10000". 2) The server-script receives the data, parse and validate them. 2b) If something sounds wrong, it returns a page containg an explicit error message and a link going to previous cached form (using javascript
21
3386
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters a zipcode that is unknown this form will open. I don't want users to modify any of this customers data until they close the zipcode form. Normally this can accomplished using a modal form, however this prevents me from opening a new copy of...
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.