VB object-oriented programming a simple demo

xiaoxiao2021-03-06  92

This example can also be considered as entry-level demonstrations using class modules.

Create a new project, add 3 text boxes in Form1. We want to limit the characters entered in these three text boxes, the first only allows you to enter numbers, the second only allows for letters, and the third only allows you to enter your uppercase letters.

General practice, you can write inspection code during the KEYPRESS event of these three text boxes. A good program is to write the check code into a shared process, let the KEYPRESS events of these three text boxes to be called.

Now, we use object-oriented ideas 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 'adds a TextBox, which is just a demo, so it simply writes it as a public member, in the actual project, generally private.

Public ISDECIMAL AS Boolean 'simply expressed such an attribute using public ISDECIMAL. The value is "Yes", only the number can be entered, "No", can only enter letters

Private sub textbox_keypress (Keyascii As Integer)

SELECT CASE Keyascii Case 0 To 31 Case 48 To 57 'Buttons for Digital If Not IsDecimal Tan Keyascii = 0 Case 65 To 90, 97 To 122' Button for Letters IF IsDecimal Then Keyascii = 0 Case Else Keyascii = 0 End Select

The END SUB class module is complete. In the Form1 code window, add the following code: Option Explicit

DIM DECITEXT AS CTXT, LetterText As ctxt, ucasetext as ctxtPrivate Sub Form_Load ()

Set Decitext = New CTXT SET DECITEXT.TEXTBOX = TEXT1 'DECITEXT.TEXTBOX Points to Text1 Decitext.Indecimal = true' Sets Decitext's ISDECIMAL properties so that Text1 can only enter numbers.

Set lettertext = new ctxt set lettertext.textbox = text2 lettertext.Indecimal = false set lettertext = new ctxt set letterText.TextBox = text3 lettetext.Indecimal = false end sub

转载请注明原文地址:https://www.9cbs.com/read-105771.html

New Post(0)