Drag a label

seanvoth

Well-known member
Joined
Aug 5, 2006
Messages
58
Programming Experience
1-3
i want to drag a laebel to a part of a sceer but when i run it, it wnt drag
 
What is a "sceer" ? Is there something wrong with you keyboard?
 
Code below allows you to relocate any control on form. Just place mouse cursor above the control, press Shift key once to capture the control, move the control around with mouse, press Shift key again to release the control. Don't use mouse buttons for this. For good measure I threw in drawing a red rectagle around the selected control.
VB.NET:
Private drag As Control
 
Private Sub frmDiv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyDown
    If e.Shift = False Then Exit Sub
    If drag Is Nothing Then
        For Each c As Control In Me.Controls
            Dim pt As Point = c.PointToClient(System.Windows.Forms.Cursor.Position)
            If c.ClientRectangle.Contains(pt) Then
                drag = c
                Me.Capture = True
                Me.Refresh()
                Exit Sub
            End If
        Next
    Else
        drag = Nothing
        Me.Capture = False
        Me.Refresh()
    End If
End Sub
 
Private Sub frmDiv_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseMove
    If drag IsNot Nothing Then
        drag.Location = e.Location
        Me.Refresh()
    End If
End Sub
 
Private Sub frmDiv_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    If drag IsNot Nothing Then
        Dim rct As Rectangle = drag.Bounds
        rct.Inflate(1, 1)
        e.Graphics.DrawRectangle(Pens.Red, rct)
    End If
End Sub
 
Back
Top