DEMO:增减控件

这个例子是在窗体上左键单击添加选中的控件,右键单击删除选中的控件。即:(有三个控件可以选择:命令按钮、文本框、标签) 当单击左边的任何一个控件,再单击右边的窗体就会出现一个控件,在单击又会出现(数字标记一次增加)当指向添加的控件单击右键时候,就会删除。

设计过程:

Ⅰ、知识要点:

①动态添加控件数组

           Load Command1(i)
           Command1(i).Left = Text2.Text
           Command1(i).Top = Text3.Text
           Command1(i).Caption = Str(i)
           Command1(i).Visible = True

②鼠标事件

③获得当前鼠标位置API函数:GetCursorPos函数

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
       X As Long
       Y As Long
End Type

 

 

Ⅱ、设计界面:

I

Ⅲ、功能代码:

Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
       X As Long
       Y As Long
End Type

Dim a As Integer
Dim b As Integer
Dim c As Integer

Private Sub Command1_Click(Index As Integer)
       a = 1
End Sub

Private Sub Command1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      If Button = 2 And Index > 0 Then
           Unload Command1(Index)              '右键删除命令按钮
      End If
End Sub

Private Sub Command2_Click()
       End
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim mouse As POINTAPI
    Dim i As Integer
        GetCursorPos mouse
        b = Val(mouse.X) * 15 - Me.Left - 400
        c = Val(mouse.Y) * 15 - Me.Top - 700
        If b > 1000 Then
            If a = 1 Then
                If Button = 1 Then
                    i = Command1.UBound + 1
                    Load Command1(i)
                    Command1(i).Left = b
                    Command1(i).Top = c
                    Command1(i).Caption = Str(i)
                    Command1(i).Visible = True
                End If
            End If
            If a = 2 Then
                If Button = 1 Then
                    i = Label1.UBound + 1
                    Load Label1(i)
                    Label1(i).Left = b
                    Label1(i).Top = c
                    Label1(i).Caption = Str(i)
                    Label1(i).Visible = True
                End If
            End If
            If a = 3 Then
                If Button = 1 Then
                    i = Text1.UBound + 1
                    Load Text1(i)
                    Text1(i).Left = b
                    Text1(i).Top = c
                    Text1(i).Visible = True
                End If
            End If
        End If
End Sub

Private Sub Label1_Click(Index As Integer)
    a = 2
End Sub

Private Sub Label1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 And Index > 0 Then
        Unload Label1(Index)                    '右键删除标签
    End If
End Sub

Private Sub Text1_Click(Index As Integer)
       a = 3
End Sub

Private Sub Text1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 And Index > 0 Then
        Unload Text1(Index)                     '右键删除文本框
    End If
End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值