|
-
May 11th, 2004, 10:00 AM
#1
Thread Starter
Junior Member
Validation?
Hello i was wondering if someone could please help me out?
I need to validate some text boxes in my vb.net program so when a user inputs the wrong data a display message is shown telling them what they need to enter.
can anyone tell me what code i would need to perform this, i have 8 text boxes in total, 2 are for text and 6 for numbers!
Thanks alot
Dave
-
May 11th, 2004, 10:24 AM
#2
Frenzied Member
Well, validation is a pretty huge subject because of all of the possibilities and different inputs that you could receive...
Instead of validating the text... I would only let them enter what you want them to by using the Mask Edit (Masked Edit maybe?) control within VB...
Using this control, you can put an "input filter" of sorts in place and only allow them to enter numbers (to a certain length even) or characters...
If you really want to get down to the nit'n'gritty of it... I would do a loop through the char array (string) (yes, they are arrays) and check each of the chars or numbers against a set of chars or numbers that you want to allow...
something like..
VB Code:
For i = 0 to string.length
If not string.char(i) > "a" and not string.char(i) < "z" ... etc.. Then
return false
end if
next i
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2004, 10:29 AM
#3
Thread Starter
Junior Member
Humm thanks sounds qute complicated, think ill just try the input filter.
what code would i need for a simple ext button?
Thanks
-
May 11th, 2004, 10:31 AM
#4
Frenzied Member
an ext button? please explain
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2004, 10:31 AM
#5
Frenzied Member
It's not really complicated... Just might take a little time to implement correctly... that's all
~Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2004, 11:19 AM
#6
Thread Starter
Junior Member
Originally posted by squirrelly1
an ext button? please explain
sorry that was meant to be an exit button
-
May 11th, 2004, 11:29 AM
#7
Simple exit?
Me.Close()
-
May 11th, 2004, 11:35 AM
#8
Thread Starter
Junior Member
lol ok thanks thats great!
-
May 11th, 2004, 11:36 AM
#9
Hyperactive Member
I use 'masked edit', but there are some little problems. I was not able to intercept keys on that control and there is not a 'readonly' property to use, like is for texboxes.
I prefer to control input by code. Here is an example of how to permit only a numeric input by Keyboard (It doesn't cover input from Ctrl+V !).
In Keypress event of your textbox:
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
That's all. Now it accepts only digits. You can do the same for letters:
If Not Char.IsLetter(e.KeyChar) Then e.Handled = True
and so on.....
If you need to accept, for example: digit, control chars, a decimal separator like "," and a limit of maximum 2 decimal digits:
VB Code:
Private Sub testi(ByRef casella As TextBox, ByVal car As Char, ByRef bool As Boolean)
If (casella.Text.IndexOf(",") >= 0 And car = ",") Then bool = True
If (Char.IsDigit(car) Or car = "," Or Char.IsControl(car)) = False Then
bool = True
End If
If (casella.Text.IndexOf(",") > 0 And Char.IsControl(car) = False) Then
If casella.Text.Substring(casella.Text.IndexOf(",")).Length > 2 Then
bool = True
End If
End If
End Sub
You can call, always in keypress event, a routine like that above, as in this example:
VB Code:
Private Sub txbEuro1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txbEuro1.KeyPress
Me.testi(txbEuro1, e.KeyChar, e.Handled)
End Sub
You have to spend a little of time, but when you'll be more confident with this approach you'll really be able to do everything you need with your textboxes
Good job
Live long and prosper (Mr. Spock)
-
May 11th, 2004, 11:48 AM
#10
Thread Starter
Junior Member
i've tried that but im getting this error back
'KeyChar' is not a member of 'System.EventArgs'.
'Handled' is not a member of 'System.EventArgs'.
the above example would work fine i just want basic validation just so that there is only text in one and numbers in the other?
Thanks
Dave
-
May 11th, 2004, 11:53 AM
#11
Frenzied Member
Why don't you just check against the ascii values of each of the characters in the string??? It's really a simple loop to implement...
Take a look at the System.Text.Ascii...whatever... object... It shouldn't take long to figure out how to use it...
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 11th, 2004, 12:01 PM
#12
Thread Starter
Junior Member
yeh i would but the code alextyx seems to be pritty straight forward!
just can't understand them errors!!!
-
May 12th, 2004, 03:15 AM
#13
Member
Do a search on the microsoft site for validatingtextboxes and there is an example class to do this. I modified this class to handle also Combo Boxes and found it very good.
-
May 12th, 2004, 05:23 AM
#14
Hyperactive Member
Dear friend, I think you missed the right event. You must put the code in KeyPress event routine, not KeyDown or KeyUP:
This first version permits digit only, the second also control chars (you need them, I think!)
VB Code:
Private Sub txbEuro1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txbEuro1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
End Sub
Private Sub txbEuro1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txbEuro1.KeyPress
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
End Sub
Try once again
I'm waiting for your positive answer
Live long and prosper (Mr. Spock)
-
May 12th, 2004, 01:18 PM
#15
Thread Starter
Junior Member
sorry Firend, what is KeyPress how do i create this??
-
May 12th, 2004, 04:48 PM
#16
Frenzied Member
It's an event.
You don't create it.
It's already there for anything that you could think about pushing a key on.
In your coding window, find the control in the left hand combo box and then find "KeyPress" in the right hand list.

Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 12th, 2004, 06:16 PM
#17
Thread Starter
Junior Member
yeah sorted that now thanks.
tried both bits of code on each the
If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
works fine for entering numbers.
i can't get the other one working just seems the same won't let me enter any text only numbers?
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
????
Thanks
Last edited by VBdotnet_newbie; May 12th, 2004 at 06:30 PM.
-
May 12th, 2004, 09:41 PM
#18
Addicted Member
If you want to enter letters use Char.IsLetter(e.KeyChar)
-
May 13th, 2004, 05:22 AM
#19
Hyperactive Member
Robymix was right! The second example I gave to you, it's useful because you can use some control keys! Good job
Live long and prosper (Mr. Spock)
-
May 17th, 2004, 10:13 AM
#20
Thread Starter
Junior Member
do u know if i could use this code
VB Code:
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
and limit the field size so only 2 digits can be entered?
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|