Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Panel2 As System.Windows.Forms.Panel
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.Panel2 = New System.Windows.Forms.Panel
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel1.Location = New System.Drawing.Point(48, 120)
Me.Panel1.Name = "Panel1"
Me.Panel1.TabIndex = 0
'
'Panel2
'
Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel2.Location = New System.Drawing.Point(376, 128)
Me.Panel2.Name = "Panel2"
Me.Panel2.TabIndex = 1
'
'CheckBox1
'
Me.CheckBox1.Location = New System.Drawing.Point(40, 296)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.TabIndex = 4
Me.CheckBox1.Text = "Release Label2"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 336)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 5
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(712, 468)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private LabelBeingMoved As Label = Nothing
Private LabelMoving As Boolean = False
Private LabelLocation As Point
Private StartX As Integer
Private StartY As Integer
Private ReleasableLabel As Label = Nothing
Private Sub Label_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
LabelBeingMoved = CType(sender, Label)
LabelLocation = New Point(LabelBeingMoved.Left, LabelBeingMoved.Top)
StartX = e.X
StartY = e.Y
LabelMoving = True
End Sub
Private Sub Label_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If LabelMoving Then
With LabelLocation
.X = .X + (e.X - StartX)
.Y = .Y + (e.Y - StartY)
End With
LabelBeingMoved.Location = LabelLocation
End If
End Sub
Private Sub Label_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
LabelMoving = False
End Sub
'Little demo to show how to allow a Label to be moved into and out of its parent
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
With ReleasableLabel
If CheckBox1.Checked Then
.Parent = Me
.BringToFront()
.Location = _
New Point(Panel2.Location.X + .Location.X, Panel2.Location.Y + .Location.Y)
Else
.Parent = Panel2
.Location = _
New Point(.Location.X - Panel2.Location.X, .Location.Y - Panel2.Location.Y)
'make sure Label is inside the Panel
If .Bottom < 0 Or .Top > Panel2.Height _
Or .Right < 0 Or .Left > Panel2.Width Then
.Left = 0
.Top = 0
End If
End If
End With
End Sub
Private Sub AddLabel(ByVal TheName As String, ByVal TheParent As Control, ByVal TheLocation As Point)
Dim lbl As New Label
With lbl
.Name = TheName
.Text = TheName
.Location = TheLocation
.BorderStyle = BorderStyle.FixedSingle
.BackColor = Color.LightBlue
End With
TheParent.Controls.Add(lbl)
AddHandler lbl.MouseDown, AddressOf Label_MouseDown
AddHandler lbl.MouseMove, AddressOf Label_MouseMove
AddHandler lbl.MouseUp, AddressOf Label_MouseUp
If TheName = "Label2" Then ReleasableLabel = lbl
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddLabel("Label1", Panel1, New Point(10, 10))
AddLabel("Label2", Panel2, New Point(20, 20))
AddLabel("Label3", Me, New Point(30, 30))
End Sub
End Class