用过Internet Explorer收藏夹的朋友都知道,利用鼠标单击时收藏夹会出现,再次单击则收藏夹消失。从编程角度讲,这是个在程序运行阶段动态增减控件的问题。以前,这仅仅是C++之类编程语言的专利。如今,微软在Visual Basic6.0中也新增了这个功能,下面就此举例进行介绍:
1.新建一个工程,窗体Form1为缺省窗体,Form1的属性项Caption=″动态增减控件的例子″。
2.加入两个命令按钮(CommandButton),其中:
Command1的属性项Caption=″增加控件″;
Command2的属性项Caption=″删除控件″。
3.加入如下代码,运行该工程,单击″增加控件″则出现新增按钮。若单击″新增按钮″时会出现对话框,表明你触发的是动态增加控件的单击事件。
Option Explicit
′通过使用 WithEvents 关键字声明一个对象变量为新的命令按钮
Private WithEvents NewButton As CommandButton
′增加控件
Private Sub Command1_Click()
If NewButton Is Nothing Then
′增加新的按钮cmdNew
Set NewButton =Controls.Add(″VB.CommandButton″, ″cmdNew″, Me)
′确定新增按钮cmdNew的位置
NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
NewButton.Caption = ″新增的按钮″
NewButton.Visible = True
End If
End Sub

VB资料大全(CHM)
