Collapsible Panel

Begginnerdev 0 Tallied Votes 2K Views Share

Here is a small extention of the Panel class that allows you to 'collapse' it.

So far, the user can place a control on the parent form and perform a collapse/expantion by:

'Where cpNew is a Collapsible Panel

If Not cpNew.PanelCollapsed Then cpNew.Collapse()

'or

If cpNew.PanelCollapsed Then cpNew.Expand()

I will be changing/improving the control as much as possible.

If anyone has any suggestions/tips they would greatly be appreciated!

Imports System.Windows.Forms

Public Class CollapsiblePanel
    Inherits Windows.Forms.Panel
    Private _isCollasped As Boolean = False

    Public ReadOnly Property PanelCollapsed As Boolean
        Get
            Return _isCollasped
        End Get
    End Property

    Private Property OriginalSize As Size


#Region "Events"
    Public Event Expanding(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs)
    Public Event Expanded(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs)
    Public Event Collapsing(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs)
    Public Event Collapsed(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs)
#End Region

    Public Sub New()
        InitializeComponent()
        Me.OriginalSize = Me.Size
    End Sub

    Public Sub Expand(Optional ByVal e As CollapsiblePanelEventArgs = Nothing)
        Try
            If Not IsNothing(e) Then
                RaiseEvent Expanding(Me, e)
            Else
                RaiseEvent Expanding(Me, New CollapsiblePanelEventArgs(False, OriginalSize))
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub Collapse(Optional ByVal e As CollapsiblePanelEventArgs = Nothing)

        If Not IsNothing(e) Then
            RaiseEvent Collapsing(Me, e)
        Else
            RaiseEvent Collapsing(Me, New CollapsiblePanelEventArgs)
        End If
        RaiseEvent Collapsing(Me, New CollapsiblePanelEventArgs)
    End Sub

    Private Sub _OnExpanding(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs) Handles Me.Expanding
        If Not e.Cancel Then
            Me.Size = e.Size
            RaiseEvent Expanded(sender, e)
        Else
            Exit Sub
        End If
    End Sub

    Private Sub _OnCollapsing(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs) Handles Me.Collapsing
        If Not e.Cancel Then
            Me.Size = e.Size
            RaiseEvent Collapsed(sender, e)
        Else
            Exit Sub
        End If
    End Sub

    Private Sub _OnExpanded(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs) Handles Me.Expanded
        _isCollasped = False
    End Sub

    Private Sub _OnCollapsed(ByVal sender As Object, ByVal e As CollapsiblePanelEventArgs) Handles Me.Collapsed
        _isCollasped = True
    End Sub

#Region "CollapsiblePanelEventArgs"
    Public Class CollapsiblePanelEventArgs
#Region "Properties"
        Public Property Cancel As Boolean = False
        Public Property Size As Size = New Size(10, 5)
        Public Property Width As Integer = Size.Width
        Public Property Height As Integer = Size.Height
#End Region

#Region "Constructors"
        Public Sub New()

        End Sub

        Public Sub New(ByVal cancel As Boolean)
            Me.Cancel = cancel
        End Sub

        Public Sub New(ByVal cancel As Boolean, size As Size)
            Me.Cancel = cancel
            Me.Size = size
        End Sub
#End Region
    End Class
#End Region
End Class
parjor 0 Newbie Poster

I am new at this. How do I incorporate the class into my project? Is this a custom control?
I am using VS2013 VB.net

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.