Option Explicit ' If you are adding an ActiveX control a ' t run-time that is ' not referenced in your project, you ne ' ed to declare it ' as VBControlExtender. Dim WithEvents ctlDynamic As VBControlExtender Dim WithEvents ctlCommand As VB.CommandButton Dim WithEvents ctlText As VB.TextBox Dim WithEvents ctlPDG As VBControlExtender Dim WithEvents ctlFlash As VBControlExtender Dim WithEvents ctlFTP As VBControlExtender Private Sub ctlCommand_Click() 'since we delcared withevents, we can us ' e them ctlDynamic.object.Value = CDate(ctlText.Text)
End Sub Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
'This is sort of an 'all-in-one' event 'so you have to check parameters and eve ' nt name Dim p As EventParameter Debug.Print Info.Name For Each p In Info.EventParameters Debug.Print p.Name, p.Value Next Select Case Info.Name Case "NewMonth" ctlText.Text = ctlDynamic.object.Value Case "Click" MsgBox ctlDynamic.object.Value End Select End Sub Private Sub Form_Load() Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1) Set ctlDynamic = Controls.Add("mscal.calendar", "calMain", Form1)
With ctlDynamic .Move 3 * 15, 30 * 15, 260 * 15, 150 * 15 .Visible = True End With With ctlText .Move 1, 1, 3400, 100 .Text = ctlDynamic.object.Value .Visible = True End With ' Add a CommandButton. Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand1", Form1)
With ctlCommand .Move 3450, 1, 450, 300 .Caption = "Go!" .Visible = True End With End Sub
'向窗体中动态增加两个图片控件(pictureBox) '并且 '将第一个图片控件做为第二个图片控件的容器Option Explicit Private WithEvents pic1 As PictureBox Private WithEvents pic2 As PictureBox
Private Sub Form_Load() Set pic1 = frmTest.Controls.Add("vb.picturebox", "pic1") Set pic2 = frmTest.Controls.Add("vb.picturebox", "pic2") Set pic2.Container = pic1 pic1.Top = 0 pic1.Left = 0 pic1.Width = 4000 pic1.Height = 3000 pic1.Visible = True pic2.Top = 300 pic2.Left = 400 pic2.Width = 3000 pic2.Height = 2000 pic2.Visible = True End Sub
dim Ocontrol as object 'Controls.add(progid as string,Name as string) 'progid :ActiveX部件的progid 'Name:ActiveX部件加载后的名称 set Ocontrol=Controls.add("VB.ComboBox","Mycom") Ocontrol.left=10 Ocontrol.Top=10 Ocontrol.visible=True '很关键的一句在Unload事件中加上 set Ocontrol=nothing
' If you are adding an ActiveX control a
' t run-time that is
' not referenced in your project, you ne
' ed to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlCommand As VB.CommandButton
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlPDG As VBControlExtender
Dim WithEvents ctlFlash As VBControlExtender
Dim WithEvents ctlFTP As VBControlExtender
Private Sub ctlCommand_Click()
'since we delcared withevents, we can us
' e them
ctlDynamic.object.Value = CDate(ctlText.Text)
End Sub
Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
'This is sort of an 'all-in-one' event
'so you have to check parameters and eve
' nt name
Dim p As EventParameter
Debug.Print Info.Name
For Each p In Info.EventParameters
Debug.Print p.Name, p.Value
Next
Select Case Info.Name
Case "NewMonth"
ctlText.Text = ctlDynamic.object.Value
Case "Click"
MsgBox ctlDynamic.object.Value
End Select
End Sub
Private Sub Form_Load()
Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
Set ctlDynamic = Controls.Add("mscal.calendar", "calMain", Form1)
With ctlDynamic
.Move 3 * 15, 30 * 15, 260 * 15, 150 * 15
.Visible = True
End With With ctlText
.Move 1, 1, 3400, 100
.Text = ctlDynamic.object.Value
.Visible = True
End With
' Add a CommandButton.
Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand1", Form1)
With ctlCommand
.Move 3450, 1, 450, 300
.Caption = "Go!"
.Visible = True
End With
End Sub
'并且
'将第一个图片控件做为第二个图片控件的容器Option Explicit
Private WithEvents pic1 As PictureBox
Private WithEvents pic2 As PictureBox
Private Sub Form_Load()
Set pic1 = frmTest.Controls.Add("vb.picturebox", "pic1")
Set pic2 = frmTest.Controls.Add("vb.picturebox", "pic2")
Set pic2.Container = pic1
pic1.Top = 0
pic1.Left = 0
pic1.Width = 4000
pic1.Height = 3000
pic1.Visible = True
pic2.Top = 300
pic2.Left = 400
pic2.Width = 3000
pic2.Height = 2000
pic2.Visible = True
End Sub
不过建议你看看联机帮助中关于controls.add()方法的使用说明,会有收获的。
'Controls.add(progid as string,Name as string)
'progid :ActiveX部件的progid
'Name:ActiveX部件加载后的名称
set Ocontrol=Controls.add("VB.ComboBox","Mycom")
Ocontrol.left=10
Ocontrol.Top=10
Ocontrol.visible=True '很关键的一句在Unload事件中加上
set Ocontrol=nothing