我讲一个 比如在窗体上定义一个lable1(0),注意,index要设置 这样就可以通过代码在窗体上根据需要显示label标签 for i=1 to 10 load label1(i) label1(i).caption=cstr(i) label1(i).top= label1(i-1).top +200 next 其他属性也可以通过代码设置 其他的控件也是如此操作
Option ExplicitPrivate WithEvents NewButton As CommandButton Private Sub Command1_Click() If NewButton Is Nothing Then Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Frame1) 'NewButton.Move Command1.Left Command1.Width + 240,Command1.Top NewButton.Move 20, 20 NewButton.Caption = "New Button" NewButton.Visible = True End If End Sub Private Sub Command2_Click() Form1.Controls.Remove NewButton End SubPrivate Sub NewButton_Click() MsgBox "New button clicked" End Sub
Dim WithEvents ctlCommand As VB.CommandButtonPrivate Sub ctlCommand_Click() MsgBox "click" End SubPrivate Sub Form_Load() Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand1", Me) '设置CommandButton的位置和尺寸ctlCommand.Top = 400 + 200 ctlCommand.Left = 20'设置CommandButton的标题 ctlCommand.Caption = "点击" '使CommandButton可见 ctlCommand.Visible = TrueEnd Sub
'先在form1上加一个text1 (0)Private Sub Form_Load() For i = 1 To 10 Load Text1(i) Text1(i).Top = 400 * i + 200 Text1(i).Left = 20 Text1(i).Visible = True Next End Sub
http://www.applevb.com/art/dyn_control.txt
比如在窗体上定义一个lable1(0),注意,index要设置
这样就可以通过代码在窗体上根据需要显示label标签
for i=1 to 10
load label1(i)
label1(i).caption=cstr(i)
label1(i).top= label1(i-1).top +200
next
其他属性也可以通过代码设置
其他的控件也是如此操作
Private Sub Command1_Click()
If NewButton Is Nothing Then
Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Frame1)
'NewButton.Move Command1.Left Command1.Width + 240,Command1.Top
NewButton.Move 20, 20
NewButton.Caption = "New Button"
NewButton.Visible = True
End If
End Sub
Private Sub Command2_Click()
Form1.Controls.Remove NewButton
End SubPrivate Sub NewButton_Click()
MsgBox "New button clicked"
End Sub
MsgBox "click"
End SubPrivate Sub Form_Load()
Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand1", Me)
'设置CommandButton的位置和尺寸ctlCommand.Top = 400 + 200
ctlCommand.Left = 20'设置CommandButton的标题
ctlCommand.Caption = "点击"
'使CommandButton可见
ctlCommand.Visible = TrueEnd Sub
For i = 1 To 10
Load Text1(i)
Text1(i).Top = 400 * i + 200
Text1(i).Left = 20
Text1(i).Visible = True
Next
End Sub