About Visual Basic 6.0 development

xiaoxiao2021-03-06  38

About Visual Basic 6.0 development

Author: Suntime · NEW YORK

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 of classes;

Class is a high-level code module that includes method, attribute, and data, which is within the scope of the module, is an Active OCX without a graphical interface. Programmers can use it as using controls, 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 speaking, if the programmed object is a group of things, then we use the standard module to be very suitable, in the following two cases, you should use the class for code processing:

(1) Create a large number of objects similar to;

(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) Realization of methods of classes;

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 rejects a password box to refuse non-alphabetical inputs:

(1) Class CLS code;

Option Explicit 'variable check

Private Withevents Mytxt As TextBox

'This class accepts and controls a Text password box

DIM Isnum as boolean

'Class module variable

Public Sub Attach (Ittext As Textbox)

'Accept external variables into mytxt

Set mytxt = ittext

End Sub

Private sub mytxt_keyup (Keycode As Integer, Shift As Integer), SHIFT AS INTEGER

ISNUM = (Keycode> = 65) and (keycode <= 90)

'Test the keyboard input of the password box is English letters

IF isnum = false kil

BEEP

mytxt.text = ""

'If the input is not an English letter, the bell and clear the password box content

Msgbox "illegal character input!"

END IF

Debug.print mytxt.text

'Debug output password box content

End Sub

'Class code ends

(2) references;

The class that has been completed can be referenced by two formats. The first way: private (public or DIM) mycls (specified class name) AS New CLS (written completed class name); second way more for the program Writing a program more than "old": First, the module-level declaration in the form code - DIM MyCls AS CLS, and then performs specific definitions during the 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 flow_load ()

Mycls.attach Text1

'Starting class

End Sub

'Remember to release resources at the end of the code

Private Sub Form_Unload (Cancel AS Integer)

Set mycls = Nothing

End

End Sub

This document shows the code writing process and call mode of the class method (although it is very similar to the event of the class), its effect is that if the non-letter is entered in the password box, the system is ringing, and deletes the password box. The original data - protects 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) Characteristics and definition of attributes of classes;

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) Attributes and basic definitions of events;

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.

(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 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 = 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

(4) Quote Programming of 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.ListIndexcase 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.

(5) Summary of programming technology for classes;

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-70690.html

New Post(0)