CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: VB.NET

Dragable Sizable UserControl Panel

567 Views
Copy Code Show/Hide Line Numbers
Public Class ctlDragableSizable
 
    ' Locked when parent control handles the resizing
    Private blnIsResizeLocationLocked As Boolean = False
 
    Private intXMouseDownPosition As Integer
    Private intYMouseDownPosition As Integer
 
    Private blnSizableVertical As Boolean = False
    Private blnSizableHorizontal As Boolean = False
    Private intIncrementVertical As Integer
    Private intIncrementHorizontal As Integer
    Private intGridLeft As Integer
    Private intGridTop As Integer
    Private blnSnapToGrid As Boolean
 
    Private enuAdjustMode As AdjustMode = AdjustMode.rNone
 
    Event Clicked(ByVal e As System.Windows.Forms.MouseEventArgs)
    Event MouseOver(ByVal pobjDragableSizable As ctlDragableSizable, ByVal pintY As Integer, ByVal pintX As Integer)
 
    Private Enum AdjustMode
        rNone = 0
        rLeft = 1
        rRight = 2
        rBottom = 3
        rTop = 4
        rMove = 5
    End Enum
 
#Region " Properties "
 
    Public Property SnapToGrid() As Boolean
        Get
            Return Me.blnSnapToGrid
        End Get
        Set(ByVal value As Boolean)
            Me.blnSnapToGrid = value
        End Set
    End Property
 
    Public Property GridTop() As Integer
        Get
            Return Me.intGridTop
        End Get
        Set(ByVal value As Integer)
            Me.intGridTop = value
        End Set
    End Property
 
    Public Property GridLeft() As Integer
        Get
            Return Me.intGridLeft
        End Get
        Set(ByVal value As Integer)
            Me.intGridTop = value
        End Set
    End Property
 
    Public Property IncrementVertical() As Integer
        Get
            Return Me.intIncrementVertical
        End Get
        Set(ByVal value As Integer)
            Me.intIncrementVertical = value
        End Set
    End Property
 
    Public Property IncrementHorizontal() As Integer
        Get
            Return Me.intIncrementHorizontal
        End Get
        Set(ByVal value As Integer)
            Me.intIncrementHorizontal = value
        End Set
    End Property
 
    Public Property SizableVertical() As Boolean
        Get
            Return Me.blnSizableVertical
        End Get
        Set(ByVal value As Boolean)
            Me.blnSizableVertical = value
        End Set
    End Property
 
    Public Property SizableHorizontal() As Boolean
        Get
            Return Me.blnSizableHorizontal
        End Get
        Set(ByVal value As Boolean)
            Me.blnSizableHorizontal = value
        End Set
    End Property
 
    Public Property BorderWidth() As Integer
        Get
            Return Me.pnlInner.Left
        End Get
        Set(ByVal value As Integer)
 
            Me.pnlInner.Left = value
            Me.pnlInner.Top = value
            Me.pnlInner.Width = Me.Width - (value * 2)
            Me.pnlInner.Height = Me.Height - (value * 2)
        End Set
    End Property
 
    Public Property ColourBorder() As Color
        Get
            Return Me.BackColor
        End Get
        Set(ByVal value As Color)
            Me.BackColor = value
        End Set
    End Property
 
    Public Property ColourInner() As Color
        Get
            Return Me.pnlInner.BackColor
        End Get
        Set(ByVal value As Color)
            Me.pnlInner.BackColor = value
        End Set
    End Property
#End Region
 
    Private Sub ctlDragableSizable_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        RaiseEvent Clicked(e)
        Me.intXMouseDownPosition = e.X
        Me.intYMouseDownPosition = e.Y
        Me.blnIsResizeLocationLocked = True
    End Sub
 
    Private Sub ctlDragableSizable_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        RaiseEvent MouseOver(Me, e.Y, e.X)
        Me.HandleMouseMove(e.Y, e.X)
    End Sub
 
    Private Sub lblInner_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlInner.MouseDown
        RaiseEvent Clicked(e)
        Me.intXMouseDownPosition = e.X
        Me.intYMouseDownPosition = e.Y
        Me.blnIsResizeLocationLocked = True
    End Sub
 
    Private Sub lblInner_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlInner.MouseMove
        RaiseEvent MouseOver(Me, Me.pnlInner.Top + e.Y, Me.pnlInner.Left + e.X)
        Me.HandleMouseMove(e.Y, e.X)
    End Sub
 
    Private Sub HandleMouseMove(ByVal pintY As Integer, ByVal pintX As Integer)
        ' Only update resize location if not in (locked) resizing mode
        If Not Me.blnIsResizeLocationLocked Then
            If Me.blnSizableHorizontal AndAlso (pintX < Me.BorderWidth OrElse pintX > (Me.Width - Me.BorderWidth)) Then
 
                If pintX < Me.BorderWidth Then
                    Me.enuAdjustMode = AdjustMode.rLeft
                Else
                    Me.enuAdjustMode = AdjustMode.rRight
                End If
 
                Cursor.Current = Cursors.SizeWE
 
            ElseIf Me.blnSizableVertical AndAlso (pintY < Me.BorderWidth OrElse pintY > Me.Height - Me.BorderWidth) Then
 
                If pintY < Me.BorderWidth Then
 
                    Me.enuAdjustMode = AdjustMode.rTop
                Else
                    Me.enuAdjustMode = AdjustMode.rBottom
                End If
 
                Cursor.Current = Cursors.SizeNS
            Else
                Me.enuAdjustMode = AdjustMode.rMove
                Cursor.Current = Cursors.SizeAll
            End If
        Else
 
            Select Case Me.enuAdjustMode
                Case ctlDragableSizable.AdjustMode.rMove
                    Me.Left -= Me.intXMouseDownPosition - pintX
                    Me.Top -= Me.intYMouseDownPosition - pintY
 
                Case ctlDragableSizable.AdjustMode.rBottom
                    If pintY >= Me.IncrementVertical Then
                        Me.Height = pintY
                    End If
 
                Case ctlDragableSizable.AdjustMode.rRight
                    If pintX >= Me.IncrementHorizontal Then
                        Me.Width = pintX
                    End If
 
                Case ctlDragableSizable.AdjustMode.rLeft
                    If (Me.Width + (pintX * -1)) >= Me.IncrementHorizontal Then
                        Me.Width += pintX * -1
                        Me.Left -= pintX * -1
                    End If
 
                Case ctlDragableSizable.AdjustMode.rTop
                    If (Me.Height + (pintY * -1)) >= Me.IncrementVertical Then
                        Me.Height += pintY * -1
                        Me.Top -= pintY * -1
                    End If
 
            End Select
        End If
 
    End Sub
 
    Public Sub HandleAutoSnapToIncrement()
 
        Select Case Me.enuAdjustMode
            Case AdjustMode.rBottom
                If Me.Height Mod Me.intIncrementVertical <> 0 Then
                    If Me.Height Mod Me.intIncrementVertical >= 5 Then
                        Me.Height = (Me.intIncrementVertical * CInt(Me.Height / (Me.intIncrementVertical + 1)))
                    Else
                        Me.Height = (Me.intIncrementVertical * CInt(Me.Height / Me.intIncrementVertical))
                    End If
                End If
 
            Case AdjustMode.rTop
                If Me.Height Mod Me.intIncrementVertical <> 0 Then
                    If Me.Height Mod Me.intIncrementVertical >= 5 Then
                        Me.Top += (Me.Height - (Me.intIncrementVertical * CInt(Me.Height / (Me.intIncrementVertical + 1))))
                        Me.Height = (Me.intIncrementVertical * CInt(Me.Height / (Me.intIncrementVertical + 1)))
                    Else
                        Me.Top -= (Me.Height - (Me.intIncrementVertical * CInt(Me.Height / (Me.intIncrementVertical + 1))))
                        Me.Height = (Me.intIncrementVertical * CInt(Me.Height / Me.intIncrementVertical))
                    End If
                End If
 
            Case AdjustMode.rLeft
                If Me.Width Mod Me.intIncrementHorizontal <> 0 Then
                    If Me.Width Mod Me.intIncrementHorizontal >= 5 Then
                        Me.Left += (Me.Width - (Me.intIncrementHorizontal * CInt(Me.Width / (Me.intIncrementHorizontal + 1))))
                        Me.Width = (Me.intIncrementHorizontal * CInt(Me.Width / (Me.intIncrementHorizontal + 1)))
                    Else
                        Me.Left -= (Me.Width - (Me.intIncrementHorizontal * CInt(Me.Width / (Me.intIncrementHorizontal + 1))))
                        Me.Width = (Me.intIncrementHorizontal * CInt(Me.Width / Me.intIncrementHorizontal))
                    End If
                End If
 
            Case AdjustMode.rRight
                If Me.Width Mod Me.intIncrementHorizontal <> 0 Then
                    If Me.Width Mod Me.intIncrementHorizontal >= 5 Then
                        Me.Width = (Me.intIncrementHorizontal * CInt(Me.Width / (Me.intIncrementHorizontal + 1)))
                    Else
                        Me.Width = (Me.intIncrementHorizontal * CInt(Me.Width / Me.intIncrementHorizontal))
                    End If
                End If
        End Select
 
    End Sub
 
    Private Sub lblInner_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlInner.MouseUp
        Me.blnIsResizeLocationLocked = False
        Me.HandleAutoSnapToIncrement()
    End Sub
 
    Private Sub ctlDragableSizable_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        Me.blnIsResizeLocationLocked = False
        Me.HandleAutoSnapToIncrement()
    End Sub
End Class
 
 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ctlDragableSizable
    Inherits System.Windows.Forms.UserControl
 
    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.pnlInner = New System.Windows.Forms.Panel
        Me.SuspendLayout()
        '
        'pnlInner
        '
        Me.pnlInner.AllowDrop = True
        Me.pnlInner.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlInner.BackColor = System.Drawing.Color.Navy
        Me.pnlInner.Cursor = System.Windows.Forms.Cursors.Default
        Me.pnlInner.Location = New System.Drawing.Point(5, 5)
        Me.pnlInner.Name = "pnlInner"
        Me.pnlInner.Size = New System.Drawing.Size(62, 30)
        Me.pnlInner.TabIndex = 0
        '
        'ctlDragableSizable
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.AutoSize = True
        Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.BackColor = System.Drawing.Color.Black
        Me.Controls.Add(Me.pnlInner)
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Me.DoubleBuffered = True
        Me.Name = "ctlDragableSizable"
        Me.Size = New System.Drawing.Size(72, 40)
        Me.ResumeLayout(False)
 
    End Sub
    Public WithEvents pnlInner As System.Windows.Forms.Panel
 
End Class
 
by Mitchell William Cooper
  January 05, 2010 @ 7:57am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate