Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello:)
Im PingLocalHostand im newbie in vb.net Programming and i dont know how to use a Class when i create a System. i like to practice a Object Oriented in vb.net . .
can any one give a some usefull sample code or function that conect to my main form . and can use as Validation for my textbox etc in my form because im a little bit Confusing . . . Thank All :)
Posted

There are many different ways to validate data in your textbox - you can use an input mask to restrict entry to certain characters, a regular expression, call a function on text changed etc...

Google vb.net textbox validation and you will have a squillion code samples...
 
Share this answer
 
Comments
PingLocalHost 31-May-11 1:57am    
Thank sir,but i want to explore a Class . . . i want all Validation in my textbox,comboBox etc all the codes are in the Class . . . i try to search google but google gave me more confusing about Class . . . Sir Please help me,Simple and Useful code i want . . . Thanks :)
Add a Class To your Project
name it myCon or Whatever
Assume that your main form name is Form1
Place Two TextBoxes on it
textBox1 and textBox2
textBox1 must only accept Alphabets
textBox2 must only accept Numbers
add the following Code On your Class :
Public Sub txtValidation(ByVal e)
        If (Asc(e.KeyChar)) < 65 Or (Asc(e.KeyChar)) > 90 And (Asc(e.KeyChar)) < 97 Or (Asc(e.KeyChar)) > 122 Then

            If (Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
    End Sub

the above Code will Restrict The user to input
any other text except alphabets.Next for Numbers only, add the Following to your Class:
Public Sub txtNumberValidation(ByVal e)
        If (Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
            e.Handled = True

        End If
        If (Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
    End Sub

After that Add a module,name it whatever and to create the object of your Class.Declare here the following Code to your module,so that you can access it anywhere in your project :
Public valid As myCon

And Finally move on to your main form Containing Textboxes
and on there keypress Event put the following Code to call the Class and the Function for the Validation
Private Sub textBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress
        valid = New myCon
        valid.txtValidation(e)


    End Sub

 Private Sub textBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox2.KeyPress

        valid = New myCon
        valid.txtNumberValidation(e)
       
    End Sub

That's all Run it ,learn it. These things are something about OOPS.for more Info Click here[^] Have Fun..
 
Share this answer
 
Comments
PingLocalHost 31-May-11 2:17am    
Thanks a lot sir :) god bless

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900