V B动态添加控件的简单
方法
SANY GROUP system office room 【SANYUA16H-
V B动态添加控件并对其进行拖放操作改变位置用Controls.Add添加控件
DimWithEventsmypicAsPictureBox
PrivateSubCommand1_Click()
Setmypic=Controls.Add("VB.PictureBox","mypic")
mypic.Left=50
mypic.Top=50
mypic.Visible=True
EndSub
至于拖放操作,在程序中设置好mypic控件的有关属性后,处理mypic的有关事件即可
注意:
如果是控件数组的话,建议采用:load控件数组名(索引)的方法添加控件
'可先手工画一个控件,Visable设置为False,Index设置为0
'若需要多个就用loadobject(index)来加载,并把它们的visable设置为true;不需要多个就真接把原有的那个的visable设成显示就行了。
PrivateDeclareFunctionReleaseCaptureLib"user32"()AsLong
PrivateDeclareFunctionSendMessageLib"user32"Alias"SendMessageA"(_
ByValhwndAsLong,ByValwMsgAsLong,_
ByValwParamAsLong,lParamAsAny)AsLong
PrivateSubCommand1_MouseDown(IndexAsInteger,ButtonAsInteger,_
ShiftAsInteger,XAsSingle,YAsSingle)
IfButton=1Then
ReleaseCapture'移动控件
SendMessageCommand1(Index).hwnd,&HA1,2,0&
EndIf
EndSub
PrivateSubForm_Click()
OnErrorGoToendsub
Fori=1To5
LoadCommand1(i)'生成控件
Command1(i).Top=Command1(i-1).Top
Command1(i).Left=Command1(i-1).Left+Command1(i-1).Width
Command1(i).Visible=True
Next
ExitSub
EndSub
‘例如:添加一个commandbutton
PrivateSubForm_Load()
"https://www.sodocs.net/doc/a73649646.html,mandButton","cmdOk",Picture1
WithForm1!cmdOk
.Visible=True
.Width=500
.Caption="确认(&Y)"
EndWith
EndSub
‘删除控件
"ctl1"ctl1为控件名