About Visual Basic 6.0 development (2)

zhaozj2021-02-17  48

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

'Transfer attribute value to the class

Set biaozhi = mvarbiaozhiend 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.

The code exemplified in this article is relatively simple, but it covers all aspects of the module programming technology, I hope that beginners can learn from, and it will also hope that the programmer can discuss together. We should believe that no matter how complex high-rise buildings are all made by ordinary, the same, whether so complicated software projects are composed of basic program statements, programming enthusiasts, programmers The difference between the analyst is just that the program constructed with the same program statement is different.

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

New Post(0)