A simple demo program for VB Object-oriented programming

Source: Internet
Author: User
Programming | program | Object This example can also be seen as an entry level demo using class modules.

Create a new project and add 3 text boxes to the Form1. We want to limit the characters that are entered in these three text boxes, the first one to allow only numbers, the second to only enter letters, and the third to allow only uppercase letters.

As a general practice, you can write the check code separately in the KeyPress event procedure for these three text boxes. A better solution is to write the inspection code in a common process, so that the KeyPress events of the three text boxes are invoked separately.

Now, we use object-oriented thinking to solve this problem.

Add a class module to the project, named Ctxt. Add the following code to its Code window:

Public WithEvents textbox as TextBox ' Add a TextBox, this is just a demo, so simply write it as a public member, in the actual project, is generally private.
Public isdecimal as Boolean ' simply represents a property of this class with public. The value is yes, you can only enter a number, no, you can only enter letters

Private Sub textbox_keypress (KeyAscii as Integer)

Select Case KeyAscii
Case 0 to 31
Case 57 ' button to number
If not isdecimal Then keyascii = 0
Case A to 122 ' button to the letter
If isdecimal Then keyascii = 0
Case Else
KeyAscii = 0
End Select

End Sub

The class module is complete.

In the Form1 Code window, add the following code:

Option Explicit

Dim Decitext as Ctxt, Lettertext as Ctxt, Ucasetext as Ctxt

Private Sub Form_Load ()

Set Decitext = New ctxt
Set Decitext.textbox = Text1 ' Decitext.textbox point to Text1
Decitext.isdecimal = True ' Sets the Isdecimal property of Decitext so that TEXT1 can only enter numbers.

Set Lettertext = New ctxt
Set Lettertext.textbox = Text2
Lettertext.isdecimal = False

Set Lettertext = New ctxt
Set Lettertext.textbox = Text3
Lettertext.isdecimal = False

End Sub

Press F5 to run, and try typing some characters into the three text boxes. Haha, we in the Form1 window, did not write any inspection code, let these three text boxes work very well.

Now, we need to let Text3 have ctxt features that only uppercase letters are allowed to enter. Simply add the following lines of code to the Form1 code form:

Private Sub text3_keypress (KeyAscii as Integer)
If keyascii > KeyAscii < 123 Then
KeyAscii = 0
End If
End Sub

F5 run to see. TEXT3 can accurately control the keys. Here, we notice that TEXT3 can respond to event procedures in the Form1 form. A procedure that acts as a member of a class and responds to the same event in a class.

Is it simple?

This is the class.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.