I am making a windows form application in visual studio 2013 Express. In order to make the application look more customized and attractive, I designed the forms in my applic
Simple as this, add this code to your Form-Class:
#Region " Move Form "
' [ Move Form ]
'
' // By Elektro
Public MoveForm As Boolean
Public MoveForm_MousePosition As Point
Public Sub MoveForm_MouseDown(sender As Object, e As MouseEventArgs) Handles _
MyBase.MouseDown ' Add more handles here (Example: PictureBox1.MouseDown)
If e.Button = MouseButtons.Left Then
MoveForm = True
Me.Cursor = Cursors.NoMove2D
MoveForm_MousePosition = e.Location
End If
End Sub
Public Sub MoveForm_MouseMove(sender As Object, e As MouseEventArgs) Handles _
MyBase.MouseMove ' Add more handles here (Example: PictureBox1.MouseMove)
If MoveForm Then
Me.Location = Me.Location + (e.Location - MoveForm_MousePosition)
End If
End Sub
Public Sub MoveForm_MouseUp(sender As Object, e As MouseEventArgs) Handles _
MyBase.MouseUp ' Add more handles here (Example: PictureBox1.MouseUp)
If e.Button = MouseButtons.Left Then
MoveForm = False
Me.Cursor = Cursors.Default
End If
End Sub
#End Region
Here is an updated version:
' ***********************************************************************
' Author : Elektro
' Modified : 15-March-2015
' ***********************************************************************
'
' Copyright (c) Elektro Studios. All rights reserved.
'
' ***********************************************************************
#Region " Option Statements "
Option Explicit On
Option Strict On
Option Infer Off
#End Region
#Region " Usage Examples "
'Public Class Form1
' '''
' ''' The instance that manages the form(s) dragging.
' '''
' Private formDragger As FormDragger = FormDragger.Empty
' Private Sub Test() Handles MyBase.Shown
' Me.InitializeDrag()
' End Sub
' Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
' Handles Button1.Click
' Me.AlternateDragEnabled(Me)
' End Sub
' Private Sub InitializeDrag()
' ' 1st way, using the single-Form constructor:
' Me.formDragger = New FormDragger(Me, enabled:=True, cursor:=Cursors.SizeAll)
' ' 2nd way, using the multiple-Forms constructor:
' ' Me.formDragger = New FormDragger({Me, Form2, form3})
' ' 3rd way, using the default constructor then adding a Form into the collection:
' ' Me.formDragger = New FormDragger
' ' Me.formDragger.AddForm(Me, enabled:=True, cursor:=Cursors.SizeAll)
' End Sub
' '''
' ''' Alternates the dragging of the specified form.
' '''
' ''' The form.
' Private Sub AlternateDragEnabled(ByVal form As Form)
' Dim formInfo As FormDragger.FormDragInfo = Me.formDragger.FindFormDragInfo(form)
' formInfo.Enabled = Not formInfo.Enabled
' End Sub
'End Class
#End Region
#Region " Imports "
Imports System.ComponentModel
#End Region
#Region " Form Dragger "
'''
''' Enable or disable drag at runtime on a .
'''
Public NotInheritable Class FormDragger : Implements IDisposable
#Region " Properties "
'''
''' Gets an collection that contains the Forms capables to perform draggable operations.
'''
''' The .
Public ReadOnly Property Forms As IEnumerable(Of FormDragInfo)
Get
Return Me.forms1
End Get
End Property
'''
''' An collection that contains the Forms capables to perform draggable operations.
'''
Private forms1 As IEnumerable(Of FormDragInfo) = {}
'''
''' Represents a instance that is Nothing .
'''
''' Nothing
Public Shared ReadOnly Property Empty As FormDragger
Get
Return Nothing
End Get
End Property
#End Region
#Region " Types "
'''
''' Defines the draggable info of a .
'''
Public NotInheritable Class FormDragInfo
#Region " Properties "
'''
''' Gets the associated used to perform draggable operations.
'''
''' The associated .
Public ReadOnly Property Form As Form
Get
Return form1
End Get
End Property
'''
''' The associated
'''
Private ReadOnly form1 As Form
'''
''' Gets the name of the associated .
'''
''' The Form.
Public ReadOnly Property Name As String
Get
If Me.Form IsNot Nothing Then
Return Form.Name
Else
Return String.Empty
End If
End Get
End Property
'''
''' Gets or sets a value indicating whether drag is enabled on the associated .
'''
''' true if drag is enabled; otherwise, false .
Public Property Enabled As Boolean
'''
''' A instance instance containing the draggable information of the associated .
'''
''' The draggable information.
Public Property DragInfo As FormDragger = FormDragger.Empty
'''
''' Gets or sets the used to drag the associated .
'''
''' The .
Public Property Cursor As Cursor = Cursors.SizeAll
'''
''' Gets or sets the old form's cursor to restore it after dragging.
'''
''' The old form's cursor.
Public Property OldCursor As Cursor = Nothing
'''
''' Gets or sets the initial mouse coordinates, normally .
'''
''' The initial mouse coordinates.
Public Property InitialMouseCoords As Point = Point.Empty
'''
''' Gets or sets the initial location, normally .
'''
''' The initial location.
Public Property InitialLocation As Point = Point.Empty
#End Region
#Region " Constructors "
'''
''' Initializes a new instance of the class.
'''
''' The form.
Public Sub New(ByVal form As Form)
Me.form1 = form
Me.Cursor = form.Cursor
End Sub
'''
''' Prevents a default instance of the class from being created.
'''
Private Sub New()
End Sub
#End Region
#Region " Hidden Methods "
'''
''' Serves as a hash function for a particular type.
'''
Public Shadows Function GetHashCode() As Integer
Return MyBase.GetHashCode
End Function
'''
''' Gets the System.Type of the current instance.
'''
''' The exact runtime type of the current instance.
Public Shadows Function [GetType]() As Type
Return MyBase.GetType
End Function
'''
''' Determines whether the specified System.Object instances are considered equal.
'''
Public Shadows Function Equals(ByVal obj As Object) As Boolean
Return MyBase.Equals(obj)
End Function
'''
''' Determines whether the specified System.Object instances are the same instance.
'''
Private Shadows Sub ReferenceEquals()
End Sub
'''
''' Returns a String that represents the current object.
'''
Public Shadows Function ToString() As String
Return MyBase.ToString
End Function
#End Region
End Class
#End Region
#Region " Constructors "
'''
''' Initializes a new instance of the class.
'''
Public Sub New()
Me.forms1={}
End Sub
'''
''' Initializes a new instance of the class.
'''
''' The used to perform draggable operations.
''' If set to true , enable dragging on the .
''' The used to drag the specified .
Public Sub New(ByVal form As Form,
Optional enabled As Boolean = False,
Optional cursor As Cursor = Nothing)
Me.forms1 =
{
New FormDragInfo(form) With
{
.Enabled = enabled,
.Cursor = cursor
}
}
Me.AssocHandlers(form)
End Sub
'''
''' Initializes a new instance of the class.
'''
''' The used to perform draggable operations.
Public Sub New(ByVal forms As IEnumerable(Of Form))
Me.forms1 = (From form As Form In forms
Select New FormDragInfo(form)).ToArray
For Each form As Form In forms
Me.AssocHandlers(form)
Next form
End Sub
'''
''' Initializes a new instance of the class.
'''
'''
''' The instance
''' that contains the reference and its draggable info.
'''
''' The current mouse coordinates.
''' The current location.
Private Sub New(ByVal formInfo As FormDragInfo,
ByVal mouseCoordinates As Point,
ByVal location As Point)
formInfo.InitialMouseCoords = mouseCoordinates
formInfo.InitialLocation = location
End Sub
#End Region
#Region " Public Methods "
'''
''' Adds the specified into the draggable collection.
'''
''' The .
''' If set to true , enable dragging on the .
''' The used to drag the specified .
''' The specified form is already added.;form
Public Function AddForm(ByVal form As Form,
Optional enabled As Boolean = False,
Optional cursor As Cursor = Nothing) As FormDragInfo
For Each formInfo As FormDragInfo In Me.forms1
If formInfo.Form.Equals(form) Then
Throw New ArgumentException("The specified form is already added.", "form")
Exit Function
End If
Next formInfo
Dim newFormInfo As New FormDragInfo(form) With {.Enabled = enabled, .Cursor = cursor}
Me.forms1 = Me.forms1.Concat({newFormInfo})
Me.AssocHandlers(form)
Return newFormInfo
End Function
'''
''' Removes the specified from the draggable collection.
'''
''' The form.
''' The specified form is not found.;form
Public Sub RemoveForm(ByVal form As Form)
Dim formInfoToRemove As FormDragInfo = Nothing
For Each formInfo As FormDragInfo In Me.forms1
If formInfo.Form.Equals(form) Then
formInfoToRemove = formInfo
Exit For
End If
Next formInfo
If formInfoToRemove IsNot Nothing Then
Me.forms1 = From formInfo As FormDragInfo In Me.forms1
Where Not formInfo Is formInfoToRemove
formInfoToRemove.Enabled = False
Me.DeassocHandlers(formInfoToRemove.Form)
Else
Throw New ArgumentException("The specified form is not found.", "form")
End If
End Sub
'''
''' Finds the instance that is associated with the specified reference.
'''
''' The .
''' The instance that is associated with the specified reference.
Public Function FindFormDragInfo(ByVal form As Form) As FormDragInfo
Return (From formInfo As FormDragger.FormDragInfo In Me.forms1
Where formInfo.Form Is form).FirstOrDefault
End Function
'''
''' Finds the instance that is associated with the specified reference.
'''
''' The name.
''' The instance that is associated with the specified reference.
Public Function FindFormDragInfo(ByVal name As String,
Optional stringComparison As StringComparison =
StringComparison.OrdinalIgnoreCase) As FormDragInfo
Return (From formInfo As FormDragger.FormDragInfo In Me.forms1
Where formInfo.Name.Equals(name, stringComparison)).FirstOrDefault
End Function
#End Region
#Region " Private Methods "
'''
''' Associates the handlers to enable draggable operations.
'''
''' The form.
Private Sub AssocHandlers(ByVal form As Form)
AddHandler form.MouseDown, AddressOf Me.Form_MouseDown
AddHandler form.MouseUp, AddressOf Me.Form_MouseUp
AddHandler form.MouseMove, AddressOf Me.Form_MouseMove
AddHandler form.MouseEnter, AddressOf Me.Form_MouseEnter
AddHandler form.MouseLeave, AddressOf Me.Form_MouseLeave
End Sub
'''
''' Deassociates the handlers to disable draggable operations.
'''
''' The form.
Private Sub DeassocHandlers(ByVal form As Form)
If Not form.IsDisposed AndAlso Not form.Disposing Then
RemoveHandler form.MouseDown, AddressOf Me.Form_MouseDown
RemoveHandler form.MouseUp, AddressOf Me.Form_MouseUp
RemoveHandler form.MouseMove, AddressOf Me.Form_MouseMove
RemoveHandler form.MouseEnter, AddressOf Me.Form_MouseEnter
RemoveHandler form.MouseLeave, AddressOf Me.Form_MouseLeave
End If
End Sub
'''
''' Return the new location.
'''
'''
''' The instance
''' that contains the reference and its draggable info.
'''
''' The current mouse coordinates.
''' The new location.
Private Function GetNewLocation(ByVal formInfo As FormDragInfo,
ByVal mouseCoordinates As Point) As Point
Return New Point(formInfo.InitialLocation.X + (mouseCoordinates.X - formInfo.InitialMouseCoords.X),
formInfo.InitialLocation.Y + (mouseCoordinates.Y - formInfo.InitialMouseCoords.Y))
End Function
#End Region
#Region " Hidden Methods "
'''
''' Serves as a hash function for a particular type.
'''
Public Shadows Function GetHashCode() As Integer
Return MyBase.GetHashCode
End Function
'''
''' Gets the System.Type of the current instance.
'''
''' The exact runtime type of the current instance.
Public Shadows Function [GetType]() As Type
Return MyBase.GetType
End Function
'''
''' Determines whether the specified System.Object instances are considered equal.
'''
Public Shadows Function Equals(ByVal obj As Object) As Boolean
Return MyBase.Equals(obj)
End Function
'''
''' Determines whether the specified System.Object instances are the same instance.
'''
Private Shadows Sub ReferenceEquals()
End Sub
'''
''' Returns a String that represents the current object.
'''
Public Shadows Function ToString() As String
Return MyBase.ToString
End Function
#End Region
#Region " Event Handlers "
'''
''' Handles the MouseEnter event of the Form.
'''
''' The source of the event.
''' The instance containing the event data.
Private Sub Form_MouseEnter(ByVal sender As Object, ByVal e As EventArgs)
Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))
formInfo.OldCursor = formInfo.Form.Cursor
If formInfo.Enabled Then
formInfo.Form.Cursor = formInfo.Cursor
' Optional:
' formInfo.Form.BringToFront()
End If
End Sub
'''
''' Handles the MouseLeave event of the Form.
'''
''' The source of the event.
''' The instance containing the event data.
Private Sub Form_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))
formInfo.Form.Cursor = formInfo.OldCursor
End Sub
'''
''' Handles the MouseDown event of the Form.
'''
''' The source of the event.
''' The instance containing the event data.
Private Sub Form_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))
If formInfo.Enabled Then
formInfo.DragInfo = New FormDragger(formInfo, Form.MousePosition, formInfo.Form.Location)
End If
End Sub
'''
''' Handles the MouseMove event of the Form.
'''
''' The source of the event.
''' The instance containing the event data.
Private Sub Form_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))
If formInfo.Enabled AndAlso (formInfo.DragInfo IsNot FormDragger.Empty) Then
formInfo.Form.Location = formInfo.DragInfo.GetNewLocation(formInfo, Form.MousePosition)
End If
End Sub
'''
''' Handles the MouseUp event of the Form.
'''
''' The source of the event.
''' The instance containing the event data.
Private Sub Form_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim formInfo As FormDragInfo = FindFormDragInfo(DirectCast(sender, Form))
formInfo.DragInfo = FormDragger.Empty
End Sub
#End Region
#Region " IDisposable "
'''
''' To detect redundant calls when disposing.
'''
Private isDisposed As Boolean = False
'''
''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
'''
Public Sub Dispose() Implements IDisposable.Dispose
Me.Dispose(True)
GC.SuppressFinalize(Me)
End Sub
'''
''' Releases unmanaged and - optionally - managed resources.
'''
'''
''' true to release both managed and unmanaged resources;
''' false to release only unmanaged resources.
'''
Protected Sub Dispose(ByVal isDisposing As Boolean)
If Not Me.isDisposed Then
If isDisposing Then
For Each formInfo As FormDragInfo In Me.forms1
With formInfo
.Enabled = False
.OldCursor = Nothing
.DragInfo = FormDragger.Empty
.InitialMouseCoords = Point.Empty
.InitialLocation = Point.Empty
Me.DeassocHandlers(.Form)
End With ' form
Next formInfo
Me.forms1 = Nothing
End If ' IsDisposing
End If ' Not Me.IsDisposed
Me.isDisposed = True
End Sub
#End Region
End Class
#End Region