-
Jun 26th, 2012, 01:55 PM
#1
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.
-
Jun 30th, 2012, 08:39 PM
#2
Re: Slideable Controls
Originally Posted by dday9
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
-
Jul 1st, 2012, 06:41 PM
#3
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.
-
Jul 1st, 2012, 08:46 PM
#4
Re: Slideable Controls
Originally Posted by dday9
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.
-
Jan 24th, 2013, 09:10 PM
#5
Re: Slideable Controls
Update!
I accidentally deleted it, so you're stuck with post #1
Last edited by dday9; Feb 27th, 2013 at 12:07 PM.
-
May 15th, 2014, 06:33 PM
#6
Lively Member
Re: Slideable Controls
Works great,
I did notice that if I set it up on the left, then compile and run my app, it works as expected, however I notice in designer of that form now, the panel fills the entire form, I have to set the dock to left again. Just a pain during design.
I added a button to my Binding Navigator, is it possible to close the panel on Mouse Leave rather then clicking the button again?
Thanks for your hard work.
LB
-
May 16th, 2014, 09:18 AM
#7
Re: Slideable Controls
I did notice that if I set it up on the left, then compile and run my app, it works as expected, however I notice in designer of that form now, the panel fills the entire form, I have to set the dock to left again. Just a pain during design.
I don't know why because I just tested it and wasn't able to reproduce the same results that you had.
I added a button to my Binding Navigator, is it possible to close the panel on Mouse Leave rather then clicking the button again?
Generate the Leave event for your panel and call InvokeSlide.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|