Sunday 15 January 2012

vb.net - How do you check if an input is a negative number in VB -



vb.net - How do you check if an input is a negative number in VB -

i trying validation checks if value in textbox integer checks if the value negative. correctly checks if value integer can't check if value negative.

note: value beingness entered number of competitions attended comps = competition etc...

dim comps integer dim value double if integer.tryparse(txtcompsentered.text, integer) value = txtcompsentered.text if value < 0 lblcompsatten.forecolor = color.red txtcompsentered.forecolor = color.red lblcompsatten.text = "no negative numbers" else lblcompsatten.forecolor = color.black txtcompsentered.forecolor = color.black lblcompsatten.text = "" end if lblcompsatten.forecolor = color.black txtcompsentered.forecolor = color.black lblcompsatten.text = "" else lblcompsatten.forecolor = color.red txtcompsentered.forecolor = color.red lblcompsatten.text = "not number" end if

i have looked @ thread didn't seem work how-to-check-for-negative-values-in-text-box-in-vb

tryparse convert input integer if succeeds - don't need both comps , value variables. here's illustration of how works:

dim comps integer dim input string = "im not integer" dim input2 string = "2" 'tryparse fails, doesn't comps < 0 comparing if integer.tryparse(input, comps) if comps < 0 'do end if else 'i'm not integer! end if 'tryparse works, goes comps < 0 comparing if integer.tryparse(input2, comps) if comps < 0 'do end if end if

vb.net validation negative-number

No comments:

Post a Comment