Pixably add control response messages in VB
In VB programming, it is often necessary to add or delete controls to the window according to different situations, and each control is also subject to
Respond to various events. In general, the control is first added to the window when designing, and writes in their various events.
Code, then set their Visible properties to false. When running, make the control displayed, but it is neither convenient.
At the same time, the program's running speed is slower due to the addition of too many controls in the window. Let's introduce you to you.
Method for dynamically adding and deleting controls during runtime
First create an engineering file, then add the following code in Form1:
Dim withevents cmdmycommand as vb.commandbutton
Option expedition
'In the definition below, the program defines a TextBox control, a CommandButton control
'And an additional control.
DIM WITHEVENTS CTLDYNAMIC AS VBCONTROLEXTENDER
DIM WITHEVENTS CTLText As Vb.TextBox
Dim withevents ctlcommand as vb.commandbutton
Dim withevents ctlcommanddel as vb.commandbutton
Private sub ctlcommanddel_click ()
DIM I as integer
'Delete the license information of the control
Licenses.Remove "Mscomctllib.treeCtrl"
IF msgbox ("Do you delete all controls", vbyesno = vbyes then
For i = 1 to form1.controls.count
Controls.remove 0
Next I
END IF
End Sub
Private sub ctlcommand_click ()
CTLText.Text = "You click on the control button"
End Sub
Private sub cTLDYNAMIC_OBJECTEVENT (Info As EventInfo)
'When you click on an entry of the tree control, you will display an entry in CTLText.
IF info.name = "click" THEN
CTLText.Text = "You click on the entry" & _
CTLDYNAMIC.Object.selectedItem.text
END IF
End Sub
Private sub flow_load ()
DIM I as integer
'Add the license information of the tree control to the license collection
'If the license already exists, it will return an error message 732
Licenses.Add "Mscomctllib.treeCtrl"
'Add a tree control in Form, if you want the tree control to be established to different
In 'containers, like a Frame control or a PictureBox control, you only need to turn controls.add
The third parameter of the 'function can be changed to a specific container name.
Set ctlenamic = controls.add ("mscomctllib.treectrl", _
"MyCTL", FORM1)
'Set the position and size of the tree control
CTLDYNAMIC.MOVE 1, 1, 2500, 3500
'Add 10 nodes in the tree control
FOR i = 1 to 10
CTLDYNAMIC.Object.nodes.add Key: = "TEST" & STR (i), _
Text: = "Test" & Str (i)
CTLDYNAMIC.Object.nodes.add relative: = "TEST" & STR (i), _
RELATIONSHIP: = 4, Text: = "Testchild" & Str (i)
Next I
'Visible to the tree control
CTLDYNAMIC.Visible = TRUE
'Join a TextBox
Set CTLText = Controls.Add ("Vb.TextBox", "CTLText1", Form1)
'Setting the location and size of TextBox
CTLText.move (CTLDYNAMIC.LIDT CTLDYNAMIC.WIDTH 50), _
1, 2500, 100
'Set the background color of the TextBox to blue and set the foreground to white
CTLText.backcolor = VBBLUE
CTLText.Forecolor = VBWHITE
'Make TextBox visible
CTLText.visible = TRUE
'Join a CommandButton
Set ctlcommand = controls.add ("vb.commandbutton", _
"CTLCOMMAND1", FORM1)
'Set the position and size of CommandButton
CTLCommand.move (CTLDYNAMIC.LEFT CTLDYNAMIC.WIDTH 50), _
CTLText.height 50, 1500, 500
'Set the title of CommandButton
CTLCommand.caption = "click"
'Make CommanDton visible
CTLCOMMAND.Visible = TRUE
'Creating a delete button
Set ctlcommanddel = controls.add ("vb.commandbutton", _
"CTLCOMMAND2", FORM1)
CTLCommandDel.move (CTLDYNAMIC.EFT CTLDYNAMIC.WIDTH 50), _
CTLText.height 650, 1500, 500
CTLCommandDel.caption = "Delete All Controls"
CTLCommanddel.visible = true
End Sub
Run the above program, you can see that the program adds three VB standard controls in the window: a TextBox and two
CommandButton also joined an extended ActiveX control: tree control. These controls can also respond to messages,
Click on the item or "Click" button in the tree control to display the appropriate content in the text box. Click "Delete
All controls button can delete all the added controls.
As can be seen from the above program, the control with the event can be defined by WitHevents, for the standard VB
Controls (such as CommandButton, TextBox, etc.) can be defined by vb.xxx, where xxx is the name of the control class.
And for extended ActiveX controls, you can define vbcontroleXtender, before loading extended controls,
First, use the license information to join the control.
The above program runs through the VB6, WIN98 Chinese version.