Results 1 to 5 of 5

Thread: Slideable Controls

  1. #1
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    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:
    -It gets a little choppy depending on how fast you want it to slide

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

    Notes:
    -This assumes that you all ready have a button, a panel, and a timer added to your form with the controls name and 1(ie - button1, panel1, and timer1). I also suggest that if you want to make the slide effect go faster or slower, then change the subtraction in the panel's size rather than changing the timer's interval. It makes it a little less choppy.


    Collapses to the left:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        Private collapsed As Boolean = False
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Panel
            Dim slidePanel As Panel = Panel1
            With slidePanel
                .Dock = DockStyle.Left
                .Width = 200
                .BorderStyle = BorderStyle.FixedSingle
            End With
    
            'Timer
            Dim tmr As Timer = Timer1
            With tmr
                .Enabled = False
                .Interval = 1
            End With
    
            'Button
            Dim btn As Button = Button1
            With btn
                .Text = "......"
                .Size = New Size(15, 75)
                .Anchor = AnchorStyles.Left
                .Location = New Point(Panel1.Width + 1, CInt(Me.Height / 2 - Button1.Height))
            End With
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If collapsed = False Then
                If Panel1.Width > 0 Then
                    Panel1.Size = New Size(Panel1.Size.Width - 5, Panel1.Size.Height)
                    Button1.Location = New Point(Panel1.Width + 1, CInt(Me.Height / 2 - Button1.Height))
                ElseIf Panel1.Width <= 0 Then
                    Timer1.Enabled = False
                    collapsed = True
                End If
            Else
                If Panel1.Width < 200 Then
                    Panel1.Size = New Size(Panel1.Size.Width + 5, Panel1.Size.Height)
                    Button1.Location = New Point(Panel1.Width + 1, CInt(Me.Height / 2 - Button1.Height))
                ElseIf Panel1.Width >= 200 Then
                    Timer1.Enabled = False
                    collapsed = False
                End If
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    End Class
    Collapses to the right:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        Private collapsed As Boolean = False
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim slidePanel As Panel = Panel1
            With slidePanel
                .Dock = DockStyle.Right
                .Width = 200
                .BorderStyle = BorderStyle.FixedSingle
            End With
    
            'Timer
            Dim tmr As Timer = Timer1
            With tmr
                .Enabled = False
                .Interval = 1
            End With
    
            'Button
            Dim btn As Button = Button1
            With btn
                .Text = "......"
                .Size = New Size(15, 75)
                .Anchor = AnchorStyles.Right
                .Location = New Point((Panel1.Location.X - btn.Width) - 1, CInt(Me.Height / 2 - Button1.Height / 2))
            End With
        End Sub
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If collapsed = False Then
                If Panel1.Width > 0 Then
                    Panel1.Size = New Size(Panel1.Size.Width - 5, Panel1.Size.Height)
                    Button1.Location = New Point((Panel1.Location.X - Button1.Width) - 1, CInt(Me.Height / 2 - Button1.Height / 2))
                ElseIf Panel1.Width <= 0 Then
                    Timer1.Enabled = False
                    collapsed = True
                End If
            Else
                If Panel1.Width < 200 Then
                    Panel1.Size = New Size(Panel1.Size.Width + 5, Panel1.Size.Height)
                    Button1.Location = New Point((Panel1.Location.X - Button1.Width) - 1, CInt(Me.Height / 2 - Button1.Height / 2))
                ElseIf Panel1.Width >= 200 Then
                    Timer1.Enabled = False
                    collapsed = False
                End If
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    End Class
    Collapses to the top:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        Private collapsed As Boolean = False
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim slidePanel As Panel = Panel1
            With slidePanel
                .Dock = DockStyle.Top
                .Height = 200
                .BorderStyle = BorderStyle.FixedSingle
            End With
    
            'Timer
            Dim tmr As Timer = Timer1
            With tmr
                .Enabled = False
                .Interval = 1
            End With
    
            'Button
            Dim btn As Button = Button1
            With btn
                .Text = "......"
                .Size = New Size(75, 15)
                .Anchor = AnchorStyles.Right
                .Location = New Point(CInt(Me.Width / 2 - Button1.Width / 2), Panel1.Height + 1)
            End With
        End Sub
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If collapsed = False Then
                If Panel1.Height > 0 Then
                    Panel1.Size = New Size(Panel1.Size.Width, Panel1.Size.Height - 5)
                    Button1.Location = New Point(CInt(Me.Width / 2 - Button1.Width / 2), Panel1.Height + 1)
                ElseIf Panel1.Height <= 0 Then
                    Timer1.Enabled = False
                    collapsed = True
                End If
            Else
                If Panel1.Height < 200 Then
                    Panel1.Size = New Size(Panel1.Size.Width, Panel1.Size.Height + 5)
                    Button1.Location = New Point(CInt(Me.Width / 2 - Button1.Width / 2), Panel1.Height + 1)
                ElseIf Panel1.Height >= 200 Then
                    Timer1.Enabled = False
                    collapsed = False
                End If
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    End Class
    Collapses to the bottom:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        Private collapsed As Boolean = False
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim slidePanel As Panel = Panel1
            With slidePanel
                .Dock = DockStyle.Bottom
                .Height = 200
                .BorderStyle = BorderStyle.FixedSingle
            End With
    
            'Timer
            Dim tmr As Timer = Timer1
            With tmr
                .Enabled = False
                .Interval = 1
            End With
    
            'Button
            Dim btn As Button = Button1
            With btn
                .Text = "......"
                .Size = New Size(75, 15)
                .Anchor = AnchorStyles.Right
                .Location = New Point(CInt(Me.Width / 2 - Button1.Width / 2), Panel1.Location.Y - Button1.Height)
            End With
        End Sub
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If collapsed = False Then
                If Panel1.Height > 0 Then
                    Panel1.Size = New Size(Panel1.Size.Width, Panel1.Size.Height - 5)
                    Button1.Location = New Point(CInt(Me.Width / 2 - Button1.Width / 2), Panel1.Location.Y - Button1.Height)
                ElseIf Panel1.Height <= 0 Then
                    Timer1.Enabled = False
                    collapsed = True
                End If
            Else
                If Panel1.Height < 200 Then
                    Panel1.Size = New Size(Panel1.Size.Width, Panel1.Size.Height + 5)
                    Button1.Location = New Point(CInt(Me.Width / 2 - Button1.Width / 2), Panel1.Location.Y - Button1.Height)
                ElseIf Panel1.Height >= 200 Then
                    Timer1.Enabled = False
                    collapsed = False
                End If
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Enabled = True
        End Sub
    End Class
    Suggestions are welcomed :]

  2. #2
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,140

    Re: Slideable Controls

    Quote Originally Posted by dday9 View Post
    Collapses to the left:
    For Collapses to the left... The button is not vertically centered on a form that has title bar/borders so you might want to use the panel height or the forms client height. You can use "\" to divide integers, and since the panel is docked and button is anchored you could just set their Width and Left properties. Anyway I like the simple idea, tho I may use a BGW instead of a timer, still playing around. Cheers

    Code:
    Public Class Form1
        Private collapsed As Boolean = False
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Panel
            With Panel1
                .Dock = DockStyle.Left
                .Width = 200
                .BorderStyle = BorderStyle.FixedSingle
            End With
            'Button
            With Button1
                .Text = "......"
                .Size = New Size(15, 75)
                .Anchor = AnchorStyles.Left
                .Location = New Point(Panel1.Width + 1, (Me.ClientSize.Height - Button1.Height) \ 2)
            End With
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim sw = Stopwatch.StartNew
            Do Until sw.ElapsedMilliseconds >= 1 ' add a short delay 
            Loop
        End Sub
    
        Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            If collapsed = False Then
                If Panel1.Width > 0 Then
                    Panel1.Width -= 1
                    Button1.Left = Panel1.Width + 1
                    BackgroundWorker1.RunWorkerAsync()
                Else
                    collapsed = True
                End If
            Else
                If Panel1.Width < 200 Then
                    Panel1.Width += 1
                    Button1.Left = Panel1.Width + 1
                    BackgroundWorker1.RunWorkerAsync()
                Else
                    collapsed = False
                End If
            End If
        End Sub
    
    End Class
    

  3. #3
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    Re: Slideable Controls

    Thanks for the suggestion. I've only done very minimal work with the bg worker. But I do like the idea and use of the background worker better than the timer.

  4. #4
    PowerPoster Edgemeal's Avatar
    Join Date
    Sep 06
    Location
    WindowFromPoint(x,y)
    Posts
    3,140

    Re: Slideable Controls

    Quote Originally Posted by dday9 View Post
    Thanks for the suggestion. I've only done very minimal work with the bg worker. But I do like the idea and use of the background worker better than the timer.
    Its really a poor example/use of a BGW , but VB timers can't run at intervals of 1ms and vary a lot.

  5. #5
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    Re: Slideable Controls

    Update!
    I accidentally deleted it, so you're stuck with post #1

Posting Permissions

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