求VB 动态填加⼀删除控件源码

2025-05-14 02:41:26
推荐回答(4个)
回答1:

在窗体中添加一个command1,一个picture1
然后在picture1上添加picture2,将其index属性改为0,即为picture2(0),再将其visible属性改为false
窗体上添加代码:
Dim nn As Integer
Private Sub Command1_Click()
If nn = 0 Then
Picture2(nn).Visible = True '将第一个picture2(0)显示
Else
Load Picture2(nn) '加载picture2控件
Picture2(nn).Visible = True '将其可见
Picture2(nn).Left = Picture2(nn - 1).Left + Picture2(nn - 1).Width + 50 '加载的新控件距前一个距离为50
End If
nn = nn + 1
End Sub
Private Sub Picture2_Click(Index As Integer)
Unload Picture2(Index)
For i = Index + 1 To nn - 1
Picture2(i).Left = Picture2(i).Left - 50 - Picture2(i).Width
Next
End Sub
Private Sub Form_Load()
nn = 0
End Sub

回答2:

'窗体上添加一个Picture控件,Name属性设为pic,Index属性设为0

'初始化Picture
Private Sub Form_Load()

pic(0).Left = -pic(0).Width

AddPic
MovePic 1, 0

End Sub

'添加按钮
Private Sub Command1_Click()
Dim i As Integer
For i = pic.UBound To 1 Step -1

If pic(i).Visible = True Then

Exit For

End If

Next

AddPic

MovePic pic.UBound, i

End Sub

'删除Picture
Private Sub pic_Click(Index As Integer)

If Index <> 0 Then pic(Index).Visible = False

SortPic

End Sub

'添加Picture
Sub AddPic()

Load pic(pic.UBound + 1)
pic(pic.UBound).Visible = True
'pic(pic.UBound).Print pic.UBound '显示index值,需要把pic的AutoRedraw设为True

End Sub

'移动Picture
Sub MovePic(ByVal ParIndex As Integer, ByVal ParPrevIndex As Integer)

pic(ParIndex).Left = pic(ParPrevIndex).Left + pic(ParPrevIndex).Width + 50

pic(ParIndex).Top = pic(ParPrevIndex).Top

End Sub

'排序Picture
Sub SortPic()
Dim i As Integer
Dim j As Integer

For i = 1 To pic.UBound

If pic(i).Visible = True Then

MovePic i, j

j = i

End If

Next

End Sub

回答3:

控件数组就可以啊
教科书上去看嘛

回答4:

这是没有问题的你要建的不是一个pitcturebox在picture而是一个picturebox的控件组。这样你就方便做了。控件组的名称是一样的用他们的index来区加他们。你试试就知道了。你要不把资源发过来我给你做。哈哈