Solved

CONTROLTOCOMPARE AND CONTROLTOVALIDATE in VB.NET

Posted on 2006-06-18
5
399 Views
Last Modified: 2008-03-10
in the compare validatore in VB.NET, there is CONTROLTOCOMPARE  and CONTROLTOVALIDATE.

can someone explain to e how we use this?

thanks
 
0
Comment
Question by:jskfan
  • 3
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 250 total points
ID: 16931798
The Compare Validator is used when you want the value in a control to be in some way related to the value in another control, say you have two text boxes Txt1 and Tx2 you want that the value in Txt2 must be smaller than thatof Txt1 in that case you would use the Compare validator and here Control to Validate would be Txt2 and Control to Compare would be Txt1
For more info look at the examples at the site
http://www.w3schools.com/aspnet/control_comparevalidator.asp
0
 

Author Comment

by:jskfan
ID: 16944368
I will test it and  get back with you later.
0
 

Author Comment

by:jskfan
ID: 16947537
In The Compare Validator there is:

ControlToCompare
ControlToValidate
Operator
ValueToCompare

there is textbox1 and textbox2

if I want textbox2 to have a number of 9 or greater than 9
and the number entered in textbox2 to be greater than the number entered in textbox1.

how should I do that?

0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 250 total points
ID: 16948531
There are two validations that you require now
1. TextBox1 should have an Integer value Greater than or equal to 9
2. TextBox2 should have an Integer value greater than TextBox1 value

So we need 2 Validation controls
1. RangeValidator
    Put a Range Validator on your fomr and set the following properties
    ControlToValidate = TextBox1
    MinimumValue = 9
    MaximumValue = 65000 '(any max value that you want)
    Type = Integer

2. CompareValidator
    Put a CompareValidator and set the following properties
    ControlToValidate = TextBox2
    ControlToCompare = TextBox1
    Operator = "Greater Than"
    Type = Integer

Now test your App....
The properties are self explanatory though I'd just briefly describe what's being done
ControlToValidate is always the control that will be validated
ControlToCompare is the control whose value would be used for comparison and the comaprison could be "Equal To", "Greater Than" etc. and this is the Operator, lastly the Type tells us what data type we are trying to compare.
Property ValueToCompare is not required here, it is used to specify a static value to compare to, i.e. in case you wanted that instead of comparing the value with the TextBox1 value use a constant value you'd use that.
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 250 total points
ID: 16948680
On the side Note, there are a few things, about validation controls, to remember
1. "No value" is a valid entry except in the case of RequiredFieldValidator
    >>   To test it, do not enter anything in the TextBox1 and click the PostBack button, it will not raise validation Error

2. If the value of the target control in the ControlToValidate property cannot be converted to the appropriate data type, validation Error is thrown
    >>  To test put a string or a char in textbox1 and check it will give validation error

3. If the value of the target control in the ControlToCompare property cannot be converted to the appropriate data type, No Error is thrown
    >> It means that if TextBox1 contained a string or a char and we said validate that TextBox2 has an Integer value greater than that in TextBox1 the result would always be "No Error" because the string in textbox1 can not be converted to Integer so no error is thrown
    To test this put a new TextBox say TextBox3, now change the CompareValidator's ControlToCompare property from textbox1 to TextBox3, Now if you put an integer in TextBox3 and test the TextBox2 value to be greater than this value it will validate properly, but if you enter a string in TextBox3 and validate it will never give Validation Error
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now