两个Label控件,我想做成的功能就是,这两个Label空间都是可以拖动的,比方说可以把Label1拖动,然后当Label1拖动到Label2上的时候,交换Label1和Label2上的内容,请问这个功能怎么实现呢?请详细说明一下,谢谢了。

解决方案 »

  1.   

    把两个Label的DragMode属性设为 AutoMatic。Private Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
        
        Dim t   As String
        
        If Source Is Label2 Then
            t = Label1.Caption
            Label1.Caption = Label2.Caption
            Label2.Caption = t
        End IfEnd SubPrivate Sub Label2_DragDrop(Source As Control, X As Single, Y As Single)
        
        Dim t   As String
        
        If Source Is Label1 Then
            t = Label2.Caption
            Label2.Caption = Label1.Caption
            Label1.Caption = t
        End If
        
    End Sub
      

  2.   

    Label控件的属性的DragMode设为手动
    Option Explicit
    Dim cx As Long, cy As Long
    Dim a(1) As String
    Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - cx, Y - cy
    End SubPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.Drag 1
    cx = X: cy = Y
    a(0) = Label1.Caption
    a(1) = Label2.Caption
    Timer1.Enabled = True
    End SubPrivate Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.Drag 2
    End SubPrivate Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label2.Drag 1
    cx = X: cy = Y
    a(0) = Label1.Caption
    a(1) = Label2.Caption
    Timer1.Enabled = True
    End SubPrivate Sub Label2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label2.Drag 2
    End SubPrivate Sub Timer1_Timer()
    If Abs(Label1.Top - Label2.Top) < Label1.Height And Abs(Label1.Width - Label2.Width) < Label1.Width Then
    Label2.Caption = a(0)
    Label1.Caption = a(1)
    Timer1.Enabled = False
    End If
    End Sub