dim label1 as new Label With label1 .caption = "fafdsa" .left = 10 .top = 10 ... End With
Private WithEvents label1 As LabelPrivate Sub Command1_Click() If label1 Is Nothing Then Set label1 = Me.Controls.Add("vb.label", "label1", Me) With label1 .Visible = True .Caption = "label1" .Left = 10 .Top = 10 End With End Sub
无效使用NEW关键字不要NEW的话 对象变量或 with未设置 ???Dim aa As LabelWith aa aa.Caption = "sdfsdf" aa.Left = 10 aa.Top = 21 End With
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
Dim WithEvents ctlLabel As VB.Label Private Sub Form_Load() Set ctlLabel = Controls.Add("VB.Label", "ctlLabel", Me) '设置ctlLabel的位置和尺寸ctlLabel.Top = 400 + 200 ctlLabel.Left = 20'设置ctlLabel的标题 ctlLabel.Caption = "标题" '使ctlLabel可见 ctlLabel.Visible = TrueEnd Sub
With label1
.caption = "fafdsa"
.left = 10
.top = 10
...
End With
If label1 Is Nothing Then Set label1 = Me.Controls.Add("vb.label", "label1", Me)
With label1
.Visible = True
.Caption = "label1"
.Left = 10
.Top = 10
End With
End Sub
对象变量或 with未设置
???Dim aa As LabelWith aa aa.Caption = "sdfsdf"
aa.Left = 10
aa.Top = 21
End With
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
Private Sub Form_Load()
Set ctlLabel = Controls.Add("VB.Label", "ctlLabel", Me)
'设置ctlLabel的位置和尺寸ctlLabel.Top = 400 + 200
ctlLabel.Left = 20'设置ctlLabel的标题
ctlLabel.Caption = "标题"
'使ctlLabel可见
ctlLabel.Visible = TrueEnd Sub