About Visual Basic 6.0 development (2)

zhaozj2021-02-16  71

About Visual Basic 6.0 development (2)

In the technical and application of Visual Basic 6.0 (on) (hereinafter referred to as above), we discussed the programming practice of classes, categories and classes of classes, in fact, the reason why class can be widely used in software engineering Application, the most important point is that it can be very convenient to encapsulate many of the programmed properties, which not only overcomes the complexity of the control (OCX) and link library (DLL) design and debugging, but also improves The simplicity and efficiency of the program code - this article will discuss the full class programming, including methods, attributes, and basic events.

(One)

Characteristics and definition of the properties 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.

(two)

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), both 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.

(three)

Programming instances of methods, events, and attributes;

The design purpose of this program is to transform all uppercases, 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 class Class1:

Option expedition

Private Withevents Mytxt As TextBox

'Parameter interface of the method

Public Enum Style

Lcaseit 'lowercase properties

Lbigit 'big write properties

NLOGOIT 'reverse sorting properties

END ENUM

'Custom enumeration, used to implement the automatic assignment of attributes

Private Mvarbiaozhi as Style

'Implement a web connection

Public Function Done () AS STRING '

The 'DONE method is used to be based on the specified enumeration property.

'Form text box makes the corresponding character conversion operation

'And return a transformed string

IF mvarbiaozhi = nlogoit then

Done = strreverse (MyTXT)

'Reverse sorting

Elseif mvarbiaozhi = lcaseit then

DONE = LCASE (MyTXT)

'Forced lowercase conversion

Else

DONE = ucase (mytxt)

'Force capital conversion

END IF

END FUNCTION

'DONE method ends

Public property let biaozhi (byval vdata as style)

'Get the value of the property

Mvarbiaozhi = vdata

End Property

Public property Get Biaozhi () AS Style 'passes the attribute value to the class

Set biaozhi = mvarbiaozhi

End Property

Public Sub Attach (Ittext As Textbox)

'Method of connecting class

Set mytxt = ittext

End Sub

Private sub coplass_initialize ()

'This event is activated when loading

Msgbox "Hello! This program shows you the skills, attributes, events, and events programming!"

End Sub

Private sub coplass_terminate ()

'This event is activated when class is unloaded

Msgbox "Hello! Remember to fill in the code after being revoked in class_terminate!"

End Sub

The code of the 'class ends

(four)

The reference programming of the form code;

Add a text control Text1, drop-down list control combo1, command button command1 (CAPTION = "start conversion"), adjust the three controls to the appropriate position.

DIM Myt as new class1

'Reference

Private sub flow_load ()

Combo1.clear

Combo1.additem "string uppercase transformation"

Combo1.addItem "string lowercase transformation"

Combo1.additem "String Reverse Sort"

Combo1.listIndex = 0

'Add attribute options in the list box

End Sub

Private submmand1_click ()

'Activate the class when the command button is pressed

myt.attach text1

'Method parameter connection

Select Case Combo1.ListIndex

Case 0

Myt.biaozhi = lbigit

Case 1

myt.biaozhi = lcaseit

Case 2

myt.biaozhi = nlogoit

End SELECT

'According to the selection of the list box, assign the value of the BIAOZHI attribute of the class.

'Note, in the programming environment, the above properties value is added

TEXT1.TEXT = Myt.done

'Return the string after the end of the sort

End Sub

Private Sub Form_Unload (Cancel AS Integer)

Set myt = Nothing

End

'Good programming habits

End Sub

How, our code looks so concise, this feeling is like using a control, not only the call you want, but it is convenient to use VB's automatic prompt function.

(Fives)

A summary of the programming technology of the class;

Strictly speaking, the class is a quite useful technology in VB programming. It is also difficult to learn and master, and the application in large software engineering is very broad and effective, but in small software development, in order to improve software The efficiency and code clarity should avoid using more type modules, controls, and join libraries to replace standard modules.

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

New Post(0)