Vb6.0动态增删控件的实现 作者: 彭凌西/张群燕 Friday, October 11 2002 5:19 PM
经常在网上和杂志上看到问vb怎样动态增加控件,笔者在编一程序时,曾经用到,下面作一个较为详细的描述,希望能给大家有所帮助。最简单的方法:控件数组。 在VB编程中有一个控件数组的概念, 通过置控件的INDEX 索引值的控制, 不仅可以建立一组具有紧密联系的相同控件, 而且可以实现在程序运行状态中增加控件数目功能, 这在实际编程中应用的非常广泛;在编程中实现具体增加控件数组如下代码即可,请先在窗体上添加一个option控件,名称为option1,并将index属性设为0.Private Sub Form_ load() Dim i As Integer For i = 1 To 5 Load Option1(i) ' 给数组添加 5 个选项按钮。 Option1(i).Top = Option1(i - 1).Top + 350’设置顶端位置 Option1(i).left= Option1(i - 1).left+800 Option1(i).Visible = True’显示增加的控件 Option1(i).caption = i Next iEnd SubPrivate Sub Option1_Click(Index As Integer)MsgBox ("您点击了index为" & Index & "的option控件.")End Sub
(二)通过Add方法实现 Add方法在Controls集合中添加一个控件并返回一个对控件的引用。Add方法的语法为:Object.Add(ProgID,Name,Container)其中Object为要添加元素的集合,ProgID为标示的字符串.可通过对象浏览器来确定,例如,CommandButton控件的ProgID是VB.CommandButton. Name是控件的名称. Container是包含添加控件的容器,可以为form或Frame控件等等。 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 '新增控件的单击事件Private Sub NewButton_Click() MsgBox "您选中的是动态增加的按钮!"End Sub
这是动态加载timer1的例子: Private WithEvents timer1 As Timer Private Sub Form_Load() Set timer1 = Controls.Add("VB.Timer", "Timer1") With timer1 .Interval = 1000 .Enabled = True End With End Sub Private Sub Timer1_Timer() MsgBox "timer" End Sub 这是动态加载text2的例子。 Option Explicit Private WithEvents Text2 As TextBox Private Sub Command1_Click() Set Text2 = Controls.Add("VB.Textbox", "text2") With Text2 .Move Me.ScaleLeft, Me.ScaleTop, Me.ScaleWidth / 2, Me.ScaleHeight / 2 .Visible = True .Text = "动态加载的VB.Textbox" End With end sub如果不是工具栏上的控件(用户控件),则麻烦一点。
Vb6.0动态增删控件的实现 作者: 彭凌西/张群燕
Friday, October 11 2002 5:19 PM
经常在网上和杂志上看到问vb怎样动态增加控件,笔者在编一程序时,曾经用到,下面作一个较为详细的描述,希望能给大家有所帮助。最简单的方法:控件数组。
在VB编程中有一个控件数组的概念, 通过置控件的INDEX 索引值的控制, 不仅可以建立一组具有紧密联系的相同控件, 而且可以实现在程序运行状态中增加控件数目功能, 这在实际编程中应用的非常广泛;在编程中实现具体增加控件数组如下代码即可,请先在窗体上添加一个option控件,名称为option1,并将index属性设为0.Private Sub Form_ load() Dim i As Integer For i = 1 To 5 Load Option1(i) ' 给数组添加 5 个选项按钮。 Option1(i).Top = Option1(i - 1).Top + 350’设置顶端位置 Option1(i).left= Option1(i - 1).left+800 Option1(i).Visible = True’显示增加的控件 Option1(i).caption = i Next iEnd SubPrivate Sub Option1_Click(Index As Integer)MsgBox ("您点击了index为" & Index & "的option控件.")End Sub
Add方法在Controls集合中添加一个控件并返回一个对控件的引用。Add方法的语法为:Object.Add(ProgID,Name,Container)其中Object为要添加元素的集合,ProgID为标示的字符串.可通过对象浏览器来确定,例如,CommandButton控件的ProgID是VB.CommandButton. Name是控件的名称. Container是包含添加控件的容器,可以为form或Frame控件等等。 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 '新增控件的单击事件Private Sub NewButton_Click() MsgBox "您选中的是动态增加的按钮!"End Sub
Private WithEvents timer1 As Timer
Private Sub Form_Load()
Set timer1 = Controls.Add("VB.Timer", "Timer1")
With timer1
.Interval = 1000
.Enabled = True
End With
End Sub
Private Sub Timer1_Timer()
MsgBox "timer"
End Sub
这是动态加载text2的例子。
Option Explicit
Private WithEvents Text2 As TextBox
Private Sub Command1_Click()
Set Text2 = Controls.Add("VB.Textbox", "text2")
With Text2
.Move Me.ScaleLeft, Me.ScaleTop, Me.ScaleWidth / 2, Me.ScaleHeight / 2
.Visible = True
.Text = "动态加载的VB.Textbox"
End With
end sub如果不是工具栏上的控件(用户控件),则麻烦一点。