Question how to validate my form?

Abid Durrani

New member
Joined
Nov 26, 2010
Messages
3
Programming Experience
Beginner
hi, i am working in vb.net 2005 i have more than 10 vb forms and each form has number of textboxes, and maskedtextboxes, so i have to validate them all. i am using this way that on keypress event of textbox i use this code
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim a As Char
a = e.KeyChar

If Char.IsSymbol(a) Then
MsgBox("Please insert only digits")
e.Handled = True
End If
If Char.IsLetter(a) Then
MsgBox("Please insert only digits")
e.Handled = True
End If
If Char.IsPunctuation(a) Then
MsgBox("Please insert only digits")
e.Handled = True
End If
end sub

but this is very lengthy code, i mean that on each form i have almost 15 to 20 textboxes, and if i use the same code on each's textbox keypress event so it will be very very lengthy code. so please assist me that how i solve this problem? and how do i use the shortest code?
 
You can also just choose the event handler for each control from Designer, use the Events view in Properties window. You can do this for a multiple selection of controls, or one at a time. The event handler can be same for all controls, and doing this automatically adds the 'control.event' to the Handles clause list in code.
 
Thnks Kulrom but i didnt get what you want to say?

hi Kulrom, i read your comments but Sorry i didnt get, i mean that if you please assist me through code that i can understand easily so it would be more understandable for me.
 
Thnks JohnH but i didnt get what you want to say?


hi JohnH i read your comments but Sorry i didnt get, i mean that if you please assist me through code that i can understand easily so it would be more understandable for me.
 
Thnks JohnH but i didnt get what you want to say?


hi JohnH i read your comments but Sorry i didnt get, i mean that if you please assist me through code that i can understand easily so it would be more understandable for me.
How to: Create Event Handlers Using the Designer

There is no code involved in this process other that the event handling code itself. Setting up the event handlers can be done without any typing.
 
I am not sure if I understand what you mean, but this may help.

I was trapping for "," in textboxes/Comboboxes and other controls in a windows form application I am developing as its data was being saved to a CSV file. The project has over 100 textboxes/comboboxes so instead of adding the code to all Control's events, I wrote a function as listed below:

Public Function NoCommaCombo(ByVal Keyz As Char, ByVal oCombobox As ComboBox) As Boolean
If (Convert.ToString(Keyz) = ",") Then Return True
Return False
End Function


and then called the function in each Textbox's event handler as needed, see below for:

Private Sub Site_Name_COBOX_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Site_Name_COBOX.KeyPress
e.Handled = NoCommaCombo(e.KeyChar, Site_Name_COBOX)
End Sub


I hope this helps. Just as mentioned above by JohnH there not a whole lot of typing once I figured it out. Just make sure to modify the code for the type of control you are working with. In this case I am showing you an example of a combobox.
 
Last edited:
Back
Top