VB中在窗体上动态生成控件的代码。

本文介绍如何使用Visual Basic进行控件的动态创建及事件处理,包括TreeCtrl控件的实例化、节点添加、点击事件响应等,并实现通过按钮触发控件删除功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dim   WithEvents   cmdMyCommand   As   VB.CommandButton   
          
Option   Explicit   
    
          
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   
                    
                  Licenses.Remove   
"MSComctlLib.TreeCtrl"   
                  
If   MsgBox("是否删除所有控件",   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   
=   "你点击的是控制按钮"   
          
End   Sub   
    
          
Private   Sub   ctlDynamic_ObjectEvent(Info   As   EventInfo)   
                  
If   Info.Name   =   "Click"   Then   
                          ctlText.Text   
=   "你点击的条目是   "   &   _   
                                          ctlDynamic.object.selecteditem.Text   
                  
End   If   
          
End   Sub   
    
          
Private   Sub   Form_Load()   
                  
Dim   i   As   Integer   
                  Licenses.Add   
"MSComctlLib.TreeCtrl"   
    
                  
Set   ctlDynamic   =   Controls.Add("MSComctlLib.TreeCtrl",   _   
                                                  
"myctl",   Form1)   
                  ctlDynamic.Move   
1,   1,   2500,   3500   
                  
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   
                  ctlDynamic.Visible   
=   True   
    
                  
Set   ctlText   =   Controls.Add("VB.TextBox",   "ctlText1",   Form1)   
                  ctlText.Move   (ctlDynamic.Left   
+   ctlDynamic.Width   +   50),   _   
                                                  
1,   2500,   100   
                  ctlText.BackColor   
=   vbBlue   
                  ctlText.ForeColor   
=   vbWhite   
                  ctlText.Visible   
=   True   
    
                  
Set   ctlCommand   =   Controls.Add("VB.CommandButton",   _   
                                                  
"ctlCommand1",   Form1)   
                  ctlCommand.Move   (ctlDynamic.Left   
+   ctlDynamic.Width   +   50),   _   
                                                  ctlText.Height   
+   50,   1500,   500   
                  ctlCommand.Caption   
=   "点击"   
                  ctlCommand.Visible   
=   True   
                    
                  
Set   ctlCommandDel   =   Controls.Add("VB.CommandButton",   _   
                                                  
"ctlCommand2",   Form1)   
                  ctlCommandDel.Move   (ctlDynamic.Left   
+   ctlDynamic.Width   +   50),   _   
                                                  ctlText.Height   
+   650,   1500,   500   
                  ctlCommandDel.Caption   
=   "删除所有控件"   
                  ctlCommandDel.Visible   
=   True   
          
End   Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值