vb动态生成控件,在VB中如何动态的去生成一个Label控件呢?

在VB中如何动态的去生成一个Label控件呢? - 故障解答 - 电脑教程网

在VB中如何动态的去生成一个Label控件呢?

日期:2006-03-28   荐:
在VB中如何动态的去生成一个Label控件呢?在VB中如何动态的去生成一个Label控件呢?谢谢我觉得还是这样方便,先放上去。caption=“”,要用的时候,赋值caption就可以了。动态生成好麻烦的。。。Option ExplicitDim ctlNew As ControlDim ctlPrev As ControlPrivate Sub lstNewControls_DblClick() Select Case lstNewControls.Text Case "Label" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.Label", "lblNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.AutoSize = True ctlNew.Caption = "Label控件" ctlNew.Visible = True Case "CommandButton" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.CommandButton", "cmdNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.Width = 1800 ctlNew.Caption = "控制按钮控件(&N)" ctlNew.Visible = True Case "TextBox" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.TextBox", "txtNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.Width = 1800 ctlNew.Height = 1200 ctlNew.Text = "TextBox控件" ctlNew.Visible = True Case "Frame" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.Frame", "fraNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.Width = 1800 ctlNew.Height = 1200 ctlNew.Caption = "Frame控件标题" ctlNew.Visible = True Case "ListBox" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.ListBox", "lstNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.Width = 1800 ctlNew.Height = 1200 ctlNew.AddItem "列表项1" ctlNew.AddItem "列表项2" ctlNew.AddItem "列表项3" ctlNew.Visible = True Case "ComboBox" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.ComboBox", "cboNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.Width = 1800 ctlNew.AddItem "列表项1" ctlNew.AddItem "列表项2" ctlNew.AddItem "列表项3" ctlNew.ListIndex = 0 ctlNew.Visible = True Case "PictureBox" If Not ctlPrev Is Nothing Then Controls.Remove ctlPrev End If Set ctlNew = frmMain.Controls.Add("VB.PictureBox", "picNew", fraNewControls) Set ctlPrev = ctlNew ctlNew.Left = 360 ctlNew.Top = 360 ctlNew.Width = 1800 ctlNew.Height = 1800 ctlNew.Visible = True End SelectEnd Sub自己看看关键的关注~用控件数组。控件数组可以在设计窗体的时候复制粘贴得到,然后在程序中修改其index值得到多个控件用控件数组较方便.用Load 和Unload可随意增减数量.同意楼上--------------------------------------------------xingnup(黑猫) ( ) 用控件数组较方便.用Load 和Unload可随意增减数量--------------------------------------------------具体怎么样随意增减呢?添加一个 Label1设置Index属性成为0用代码 Load Label1(索引) 就是增加UnLoad Label1(索引) 减掉LBound、UBound 属性示例该例子为控件数组打印以上提及的两个属性的值。在一个窗体上放一个 OptionButton 控件,并设置其 Index 属性为 0 (用来创建一个控件数组)。要试用此例,可以将该代码粘贴到一个窗体的声明部分中,然后按下 F5 键并单击此窗体。Private Sub Form_Paint () Static FlagFormPainted As Integer If FlagFormPainted <> True Then '当窗体进行首次绘画时。 For i = 1 To 3 Load Option1(i) '在数组中加入三个选项按钮。 Option1(i).Top = Option1(i - 1).Top 350 Option1(i).Visible = True Next I For I = 0 to 3 '在选项按钮上放置标题。 Option1(i).Caption = "Option #" & CStr(i) Next I Option1(0).Value = True '选中第一个选项按钮。 FlagFormPainted = True '窗体已完成绘画。 End IfEnd SubPrivate Sub Form_Click () Print "Control array's Count property is " & Option1().Count Print "Control array's LBound property is " & Option1().LBound Print "Control array's UBound property is " & Option1().UBoundEnd Sub用数组好!但问题的关键是:需要设置所显示的Lable的位置!!!如果不设置好,那所有显示的Lable都会重叠在一起,给人的感觉就好象是只有一个!用Lable的位置属性(top,left,height),一般只要定义3个点就可以了用循环就能设定数组控件位置for i =1 to 3 label(i).top=i*100next i
标签: