For programmers and programming enthusiasts, the technology in VB is a difficult point in learning. During the development of large software, the module (Moudle), Active OCX, Link Link (Active DLL) and class ( Class Moudle) constitutes a systematic, efficient software engineering, and the class's technology is the basis of control and link library technology, so the theoretical and programming methods of mastery are very meaningful. (1) Basic definition and application overview; class is an advanced code module including methods, attributes, data members, which are both within the category of the module, and an Active OCX without a graphical interface, programmers can use The control is the same as it, but can't see it, it is worth noting that the class cannot be inherited. Class enables us to efficiently complete complex operations for one or a few specific objects, the action of the object is the method of the class, the object's properties are the properties of the class. Relatively, if the programmed object is a group of things, then we use the standard module to be very suitable. In both cases, you should use the class to perform code processing: (1) Creating a large number of properties; (2) Improve the encapsulation of the code. The creation of the class is very simple. When you write, you can add a blank class when you select the Add Category item in the Project menu. Class files are typically saved as extensions in .cls. (2) Implementation of the method of the class; the method of class is similar to the interface function of the dynamic link library, which can accept the specified type parameters of other form code and pass to the class. Generally, the method of the class is to specify whether there is a return value. It is usually a public process in the class.
Please see the following code example, which makes a password box refuses the non-letter input: (1) Class CLS code; Option Explicit 'Variable Check Private WitHevents MyTXT AS TEXTBOX' This class is accepted and controls a TEXT password box DIM isNUM As Boolean 'module-level variable class Public Sub Attach (itTEXT As TextBox)' receive external variables to myTxt the Set mytxt = itTEXT End Sub Private Sub mytxt_KeyUp (KeyCode As Integer, Shift As Integer) isNUM = (KeyCode> = 65) AND (Keycode <= 90) 'Testing the keyboard input of the password box is English alphabet IF isnum = false dam mytxt.text = "" If the input is not English letters, the ring is not in the English, and the password box content msgbox "illegal characters are entered! "End if debug.print mytxt.text 'debug output password box content END SUB' class code end (2) Class reference; the class that has been written can be referenced by two formats, the first way: private Or DIM) Mycls (specified class name) AS New CLS (written completed class name); more than the programmer for program writing Style is "old": first modular grade statement in the form code - -Dim mycls as CLS, then perform specific definitions during specific code - SET MyCls = New CLS . In terms of the efficiency and code of the code, there may be differences, but there is no special feeling in the author's programming practice, but I use the first way because it is more convenient to write. In addition, when the code is ended, the resource occupied by using set mycls = nothing is a very good program habit.
In the form form1 (the form has a password box control Text1, passworldchar = "*") Add the following code: Option expedition private mycls as new cls' Reference CLS Private Sub Form_Load () Mycls.attach Text1 'Startup Sub' Remember to release the resource private subform_unload (Cancel AS Integer) set mycls = Nothing end and this code shows the code writing process and call mode of the class method (although it is very similar to the class's event), its effect Yes, if the non-letter is entered in the password box, the system rings and deletes the original data in the password box - protect the password to a certain extent. The method of the class can not require any parameters, which is similar to a PUBLIC function or process, which is also the most widely used in the class. In the next article I will discuss, how to use the class's properties, events, and methods for comprehensive programming. We discussed programming practices for classes, categories and classes of classes, in fact, the reason why the class can be widely used in software engineering, the most important point is that it can be very convenient to encapsulate many programmed properties, this Not only makes programmers overcome the complexity in the control (OCX) and link library (DLL) design and debugging, but also enhances the simplicity and efficiency of program code - this article will discuss the full class programming, including method , Attributes and basic events. (1) Features and definitions of the attributes of the class; similar to the properties of the standard control, the attributes of the class allow the user to assign values within the specified data range, which is shared by each code section within the class. The acquisition and delivery of the property needs to be programmed through the Property Let and the Property Get statement. Of course, we first need to define the corresponding variable definition of global or module-level in the class. (2) The attributes and basic definitions of the event; similar to the form of the form, the class also has two basic events, class_initialize (triggering time triggered) and class_terminate (triggered when unloading), these two events are private. In fact, we can ignore these two events - as long as you remember to improve the methods and properties of the class. The class can also define your own events. It is similar to the program's programming format, but only the WitHevents keyword is required to perform parameter declaration, and the event cannot have any named parameters or optional parameters, which has no return value. In fact, the structure and attributes of the structure can be fully replaced with complex classes of the structure. (3) Programming examples of classes, events, and attributes; the design purpose of this program is to transform all uppercase, lowercase and reverse sorting of the content of the text box in the class control form. In order to facilitate the writing and call of the code, I reference the enumeration programming method in the class.
The following code is in the class class1: Option Explicit Private WitHevents MyTXT AS TEXTBOX 'method's parameter interface public enum style Lcaseit' lowercase property LPIGIT 'uppercase properties NLOGOIT' reverse sorting property end enum 'custom enumeration, used to implement an automatic assignment of the property Private MVARBIAOZHI AS STYLE 'Implementing the Upset Done () AS String' 'DONE method for the specified enumeration done (), the' form text box is used to perform the corresponding character conversion operation 'and return a transformed string If mvarBiaozhi = Nlogoit Then dONE = StrReverse (myTXT) 'reverse order ElseIf mvarBiaozhi = Lcaseit Then dONE = LCase (myTXT)' forced lowercase conversion Else dONE = UCase (myTXT) 'mandatory capitalization conversion end If end Function' dONE method ends Public Property LET BIAOZHI (BYVAL VDATA AS STYLE) 'Gets the value of the property mvarbiaozhi = vData End Property Public Property Get Biaozhi () AS STYLE' Transfer Attribute Value to the class set biaozhi = mvarbiaozh I end Property Public Sub Attach (Ittext As Textbox) Method SET MyTxt = ItText End Sub Private Sub Class_Initialize () This event activates msgbox when loading MsgBox "Hello! This program shows you the skills of using classes, attributes, attributes! "End Sub Private Sub Class_Terminate () 'This event activates msgbox when the class is uninstalled." Hello! Remember the code after being revoked in the class_terminate! "End Sub 'Class All End (4) Form Code Reference Programming; Add a text control Text1, drop-down list control combo1, command button Command1 (CAPTION =" start transformation "in form form1, adjusting three controls To the appropriate location.