VB6 is liked by many friends because of its simplicity and ease of use. But others say it's a simple feature that doesn't give developers enough room to play. such as the inability to easily inherit existing controls and derive their own controls. What Write an ActiveX control? It's too much trouble. It takes a lot of time to design and implement the interface, and you don't want your software to be released with a bunch of OCX controls? In fact, there is still a way, we can use the class module in VB6 to implement special control and event response to the control. This article describes using a class module to turn a normal label control into a form dynamic partition bar.
Figure 1 A form with a separator bar
First class module
A class module is actually a definition of an object that encapsulates a number of properties and methods that require an instance to be generated before use:
’生成类模块clsTest的一个实例test
Dim test as new clsTest
You can then use the method of the class module:
test.DoSomthing() ’调用test 的方法DoSomthing()
A simple example
Make a mouse move up and automatically get the focus and the "smart" edit box with the contents selected.
1. Create a new project, in the [Engineering] menu, select [Add Class Module], add a class module, change its name to Clstest.
2, into the class module editing interface (Figure 2).
Figure 2 Editing a class module
In the Drop-down box on the left, select generic and type the following code:
’定义一个带事件的文本框变量
Dim WithEvents MyText As TextBox
’保存文本框是否获得焦点的布尔变量
Dim bSetted As Boolean
’自己定义的类模块的方法,传入参数是文本框。
Public Sub BindText(t As TextBox)
’将文本框变量设置为传入的文本框,即是对传入文本框的引 用
Set MyText = t
End Sub
3, in the left Drop-down box to select "Class" In the right Drop-down box to select Initialize, this is the class module initialization event, you can write your own initialization code here. VB will automatically add an event subroutine for us. All we have to do is add our own code to it. In fact, this step has little effect, but it is a good habit to initialize variables.
Private Sub Class_Initialize()
’将文本框变量初始化Nothing
Set MyText = Nothing
bSetted = False
End Sub
4. Select "MyText" in the left Drop-down box, notice that it is the text box variable with the event that we defined in step 2nd. VB added it in, and then to the right Drop-down box to click the Drop-down button, hehe, see what? Turns out to be all the events of our familiar TextBox! But the mytext text box here doesn't actually exist, it's just a generation
Number, waiting for you to give it an actual text box.
Add the incident response code, so I don't have to say it.
Private Sub MyText_GotFocus()
bSetted = True
End Sub
Private Sub MyText_LostFocus()
bSetted = False
End Sub
’鼠标在控件上移动时,如果还没设置焦点,将 它设为焦点,
’并将内容选中
Private Sub MyText_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (Not bSetted) Then
MyText.SetFocus
MyText.SelStart = 0
MyText.SelLength = 9999
End If
End Sub
As of this, the class module is declared complete, and it is saved (. cls file).
5, the following is the use of class modules, very simple. Now put 3 textbox controls on a form named Text1, Text2, and Text3.
Figure 3 Test class module