vb中如何动态添加删除控件
PrivateDeclareFunctionGetCursorPosLib"user32"(lpPointAspointapi)AsLongPrivateTypepoin...
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 Single)
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 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 Single)
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
无法在窗体上添加控件是为什么
小弟明白,但是我上面的程序错在什么地方,无法显示控件啊,谢谢! 展开
3个回答
展开全部
用 Controls.Remove(控件) 动态删除控件。注意:在VB中只能动态删除那些动态添加的控件;不允许删除在窗体设计器上布置的控件!
示例如下:
(1)创建一个VB工程
(2)在Form1上布置两个Command
(3)窗体代码
Option Explicit
' 声明要被动态添加/删除的控件
Dim x As Label
'-----------------------------
' 动态添加一个Label控件
'-----------------------------
Private Sub Command1_Click()
If x Is Nothing Then
Set x = Controls.Add("VB.Label", "label1")
x.Move 150, 150
x.AutoSize = True
x.Caption = "这个是动态添加的标签"
x.Visible = True
End If
End Sub
'-----------------
' 动态删除控件
'-----------------
Private Sub Command2_Click()
If x Is Nothing Then Exit Sub
Controls.Remove x
Set x = Nothing
End Sub
Private Sub Form_Load()
Command1.Caption = "添加控件"
Command2.Caption = "删除控件"
End Sub
(4)运行
窗体启动
点击“添加控件”按钮
点击“删除控件”按钮
展开全部
动态添加控件有两种方法,一种是添加一个控件,一种是再已存在的控件组里动态添加组员。
法1:dim withevents CMD as commandbutton
set CMD=controls.add(""vb.commandbutton","CMD1")
之后设置CMD1的各个属性即可
法2:先在设计窗口上创建一个控件数组组员,比如名为CMD1(0)的一个按钮,然后在程序中使用Load方法加载新组员,比如 load CMD1(1),load CMD1(2)...之后再设置其属性即可。
乘以15是坐标两种单位的换算,VB默认坐标单位是tiwp(缇),缇与像素之间换算关系是 : 15缇=1像素
法1:dim withevents CMD as commandbutton
set CMD=controls.add(""vb.commandbutton","CMD1")
之后设置CMD1的各个属性即可
法2:先在设计窗口上创建一个控件数组组员,比如名为CMD1(0)的一个按钮,然后在程序中使用Load方法加载新组员,比如 load CMD1(1),load CMD1(2)...之后再设置其属性即可。
乘以15是坐标两种单位的换算,VB默认坐标单位是tiwp(缇),缇与像素之间换算关系是 : 15缇=1像素
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_Load()
Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
With Form1!cmdObj1
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
End Sub
Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
With Form1!cmdObj1
.Visible = True
.Width = 2000
.Caption = "Dynamic Button"
End With
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询