Results 1 to 7 of 7

Thread: Slideable Controls

Threaded View

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,372

    Slideable Controls

    Update!
    For update see post #5



    I've been seeing a few questions about a slideable control such as panels and pictureboxes. So I've decided to do my take on a slideable panel.

    Features:
    -Slides a panel

    Drawbacks:
    -None that I can tell.

    Plans:
    -None, it's a simple little code to help out those that need it

    Code:
    Option Strict On
    Option Explicit On
    Public Class SlidePanel
        Inherits Panel
    
    #Region "Globals"
    
        Private WithEvents bgworker As New System.ComponentModel.BackgroundWorker
        Private _slide As Direction = Direction.Up
        Private max As Integer = 100
        Private collapsed As Boolean = False
        Private dockstate As DockStyle = DockStyle.Top
        Private _speed As Integer = 1
    
    #End Region
    
    #Region "Properties/Enums"
    
        Public Enum Direction
            Up
            Down
            Left
            Right
        End Enum
    
        Public Property SlideDirection() As Direction
            Get
                Return _slide
            End Get
            Set(ByVal value As Direction)
                _slide = value
                Select Case _slide
                    Case Direction.Up
                        Me.Dock = DockStyle.Top
                    Case Direction.Down
                        Me.Dock = DockStyle.Bottom
                    Case Direction.Left
                        Me.Dock = DockStyle.Left
                    Case Direction.Right
                        Me.Dock = DockStyle.Right
                End Select
            End Set
        End Property
    
        Public Property Maximum() As Integer
            Get
                Return max
            End Get
            Set(ByVal value As Integer)
                max = value
            End Set
        End Property
    
        Public ReadOnly Property IsCollapsed() As Boolean
            Get
                Return collapsed
            End Get
        End Property
    
        Public Property Speed() As Integer
            Get
                Return _speed
            End Get
            Set(ByVal value As Integer)
                _speed = value
            End Set
        End Property
    
    #End Region
    
    #Region "Methods"
    
        Public Sub InvokeSlide()
            bgworker.RunWorkerAsync()
        End Sub
    
        Public Sub CancelSlide()
            bgworker.CancelAsync()
        End Sub
    
    #End Region
    
    #Region "Backgroundworker Events"
    
        Private Sub bgworker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgworker.DoWork
            If _speed < 0 Then
                bgworker.CancelAsync()
            End If
    
            Dim sw As Stopwatch = Stopwatch.StartNew
            Do Until sw.ElapsedMilliseconds >= Speed
            Loop
        End Sub
    
        Private Sub bgworker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgworker.RunWorkerCompleted
    
            If collapsed = False Then
                Select Case _slide
                    Case Direction.Up
                        If Me.Height > 0 Then
                            Me.Height -= 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                    Case Direction.Down
                        If Me.Height > 0 Then
                            Me.Height -= 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                    Case Direction.Left
                        If Me.Width > 0 Then
                            Me.Width -= 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                    Case Direction.Right
                        If Me.Width > 0 Then
                            Me.Width -= 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                End Select
            Else
                Select Case _slide
                    Case Direction.Up
                        If Me.Height < max Then
                            Me.Height += 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                    Case Direction.Down
                        If Me.Height < max Then
                            Me.Height += 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                    Case Direction.Left
                        If Me.Width < max Then
                            Me.Width += 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                    Case Direction.Right
                        If Me.Width < max Then
                            Me.Width += 1
                            bgworker.RunWorkerAsync()
                        Else
                            collapsed = Not (collapsed)
                        End If
                End Select
            End If
        End Sub
    
    #End Region
    
    #Region "Control Events/Contructor"
    
        Public Sub New()
            bgworker.WorkerSupportsCancellation = True
            Me.Dock = DockStyle.Top
        End Sub
    
        Private Sub SlidePanel_DockChanged(sender As Object, e As System.EventArgs) Handles Me.DockChanged
            Select Case Me.Dock
                Case DockStyle.Top
                    dockstate = DockStyle.Top
                    _slide = Direction.Up
                Case DockStyle.Bottom
                    dockstate = DockStyle.Bottom
                    _slide = Direction.Down
                Case DockStyle.Left
                    dockstate = DockStyle.Left
                    _slide = Direction.Left
                Case DockStyle.Right
                    dockstate = DockStyle.Right
                    _slide = Direction.Right
                Case DockStyle.Fill
                    Me.Dock = dockstate
            End Select
        End Sub
    
    #End Region
    
    End Class
    Suggestions are welcomed :]

    Update!
    I've updated the control a bit. I use a backgroundworker in the same way Edgemeal does. Also a property you should look out for is the speed property. The lower the integer, the faster it slides.
    Last edited by dday9; May 31st, 2013 at 02:26 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width