vb中如何动态添加删除控件
Private Declare Function GetCursorPos Lib "user32" (lpPoint As pointapi) As Long
Private Type pointapi
X As Long
Y As Long
End Type
Private Sub Command_Click(Index As Integer)
Text3.Text = 1
End Sub
Private Sub Command_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As S If Index > 0 And Button = 2 Then
Unload Command(Index)
End If
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim mouse As pointapi
GetCursorPos mouse
Text1.Text = Val(mouse.X) * 15 - Me.Left - 50
Text2.Text = Val(mouse.Y) * 15 - Me.Top - 300
If Text1.Text > 1000 Then
If Text3.Text = 1 Then
If Button = 1 Then
i = Command.UBound + 1
Load Command(i)
Command(i).Left = Text1.Text
Command(i).Top = Text2.Text
Command(i).Caption = Str(i)
Command(i).Visible = True
End If
End If
If Text3.Text = 2 Then
If Button = 1 Then
i = Label.UBound + 1
Load Label(i)
Label(i).Left = Text1.Text
Label(i).Top = Text2.Text
Label(i).Caption = Str(i)
Label(i).Visible = True
End If
End If
If Text3.Text = 3 Then
If Button = 1 Then
i = Text.UBound + 1
Load Text(i)
Text(i).Left = Text1.Text
Text(i).Top = Text2.Text
Text(i).Visible = True
End If
End If
End If
End Sub
Private Sub Label_Click(Index As Integer)
Text3.Text = 2
End Sub
Private Sub Label_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single If Index > 0 And Button = 2 Then
Unload Label(Index)
End If
End Sub
Private Sub Text_Click(Index As Integer)
Text3.Text = 3
End Sub
Private Sub Text_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If Index > 0 And Button = 2 Then
Unload Text(Index)
End If
End Sub
无法在窗体上添加控件是为什么
问题补充:
小弟明白,但是我上面的程序错在什么地方,无法显示控件啊,谢谢
推荐答案
Private Sub Form_Load()
Form1.Controls.Add "mandButton", "cmdObj1", Frame1
With Form1!cmdObj1
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
End Sub
其他回答共1条
动态添加控件有两种方法,一种是添加一个控件,一种是再已存在的控件组里动态添加组员。
法1:dim withevents CMD as commandbutton
set CMD=controls.add(""mandbutton","CMD1")
之后设置CMD1的各个属性即可
法2:先在设计窗口上创建一个控件数组组员,比如名为CMD1(0)的一个按钮,然后在程序中使用Load方法加载新组员,比乘以15是坐标两种单位的换算,VB默认坐标单位是tiwp(缇),缇与像素之间换算关系是: 15缇=1像素
Single, Y As Single)
e, Y As Single) As Single, Y As Single)
Load方法加载新组员,比如 load CMD1(1),load CMD1(2)...之后再设置其属性即可。