用代码添加控件?怎么写?

huhanshan013 2003-09-02 08:02:50
可以用代码实现

添加控件吗?

怎么写啊?
例如添加:

label1

怎么写?
...全文
241 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihonggen0 2003-09-03
  • 打赏
  • 举报
回复
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 = True

End Sub
lihonggen0 2003-09-03
  • 打赏
  • 举报
回复
Dim WithEvents ctlCommand As VB.CommandButton

Private Sub ctlCommand_Click()
MsgBox "click"
End Sub

Private 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 = True

End Sub
huhanshan013 2003-09-02
  • 打赏
  • 举报
回复
3楼的大哥,你的可以啊
但是看不懂前面三句的意思?
huhanshan013 2003-09-02
  • 打赏
  • 举报
回复
无效使用NEW关键字

不要NEW的话
对象变量或 with未设置
???

Dim aa As Label

With aa

aa.Caption = "sdfsdf"
aa.Left = 10
aa.Top = 21


End With
northwolves 2003-09-02
  • 打赏
  • 举报
回复
Private WithEvents label1 As Label

Private 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
txlicenhe 2003-09-02
  • 打赏
  • 举报
回复
dim label1 as new Label
With label1
.caption = "fafdsa"
.left = 10
.top = 10
...
End With

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧