关于动态加载控件的问题
我在窗口上放了一个sstab1,然后写了如下的代码
dim pic1 as picturebox
Private Sub SSTab1_Click(PreviousTab As Integer)
Dim cn As Integer
For cn = 0 To Controls.count - 1
If Controls(cn).Name = "pic1" Then
Controls.Remove "pic1"
Exit For
End If
Next cn
Set pic1 = Controls.Add("vb.picturebox", "pic1", SSTab1)
pic1.Move 120, 360, 1000, 1000
pic1.Visible = True
End Sub
发现这个程序在tab之间切换的时候会造成vb出错关闭,就这几行代码也不知道问题出在什么地方,各位大哥大姐帮我看看啊!
[解决办法]
Private Sub SSTab1_Click(PreviousTab As Integer)Dim pic1 As PictureBox'pic1改为局部变量就行了Dim cn As Integer For cn = 0 To Controls.Count - 1 If Controls(cn).Name = "pic1" Then Controls.Remove cn Exit For End If Next cn Set pic1 = Controls.Add("vb.picturebox", "pic1", SSTab1) pic1.Move 120, 360, 1000, 1000 pic1.Visible = TrueEnd Sub
[解决办法]
Private Sub SSTab1_Click(PreviousTab As Integer)
Dim pic1 As PictureBox
Dim cn As Integer
For cn = 0 To Controls.Count - 1
If Controls(cn).Name = "pic1" Then
Controls.Remove "pic1"
Exit For
End If
Next cn
Set pic1 = Controls.Add("vb.picturebox", "pic1", SSTab1)
pic1.Move 120, 360, 1000, 1000
pic1.Visible = True
End Sub
[解决办法]
'为了实现这功能,完全没必要删了加、加了删。
'可以在窗体上入一个picture1,然后如下做:
Private Sub Form_Load()
Set Picture1.Container = SSTab1
Picture1.Move 120, 360, 1000, 1000
End Sub
Private Sub SSTab1_Click(PreviousTab As Integer)
Set Picture1.Container = SSTab1
Picture1.Move 120, 360, 1000, 1000
End Sub
[解决办法]
LZ的代码实质上是每点一次TAB,就删掉pic1,然后再添加pic1,这样频繁地动态添加和删除控件,在VB里会造成一些未知错误,倒不如chinaboyzyq所说,一次性加载和销毁。