如何vb中动态添加控件

来源:百度知道 编辑:UC知道 时间:2024/06/08 19:30:40
比如,在程序运行时,我想在窗体或者picturebox中动态添加多个textbox控件,在每次点击右键时出现不同的textbox,如果textbox中没有内容,则在失去焦点时就自动卸载。。。愁啊……各位大侠请伸出您无私的爱之手来援助下吧……不胜感激
各位大哥提供的答案在form中均可用,我原来也是这么做的
但是现在要改到frame或者picturebox下,不管用了
还请诸位在帮个忙……

先在form上加两个Command按钮。Command1的Caption为添加控件;Command2的Caption为删除控件。

加入下面代码

Option Explicit
Private WithEvents newbutton As CommandButton
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮

'----------添加按钮------------
Private Sub Command1_Click()
If newbutton Is Nothing Then
Set newbutton = Controls.Add("vb.commandbutton", "cmdnew", Form1)
newbutton.Move Command1.Left + Command1.Width + 240, Command1.Top

newbutton.Caption = "动态添加的按钮"
newbutton.Visible = True
End If
End Sub

Private Sub Command2_Click()
If newbutton Is Nothing Then
Exit Sub
Else
Controls.Remove newbutton
Set newbutton = Nothing
End If
End Sub

Private Sub newbutton_click()
MsgBox "单击“删除控件”按钮删除它", vbDefaultButton1, "click"
End Sub

至于你说的功能,用这个稍微改一下就可以了

Dim i As Integer

Private Su