CollapsiblePanel (折り畳み可能パネル)

# CollapsiblePanel

需要があるかどうかわかりませんが、、、、
折り畳み可能パネルを作成しましたので、そのメモ

プロジェクトにユーザーコントロールを追加
デザイナには何も追加しない
以下のコードを貼り付け

テストできていない部分がありますので改変はご自由に


```lang:CollapsiblePanel.vb
Imports System.Windows.Forms
Imports System.Drawing

    Public Class CollapsiblePanel
        Inherits System.Windows.Forms.Panel

        Private _isCollasped As Boolean = False

        Public ReadOnly Property IsCollasped 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()

            OriginalSize = Me.Size

        End Sub

        Public Sub SetOriginalSize(ByVal value As Size)
            OriginalSize = value
        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
            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
                Return
            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
                Return
            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

```
```lang:CollapsiblePanel.Designer.vb
   <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class CollapsiblePanel
        Inherits System.Windows.Forms.Panel

        'UserControl はコンポーネント一覧をクリーンアップするために dispose をオーバーライドします。
        <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

        'Windows フォーム デザイナーで必要です。
        Private components As System.ComponentModel.IContainer

        'メモ: 以下のプロシージャは Windows フォーム デザイナーで必要です。
        'Windows フォーム デザイナーを使用して変更できます。  
        'コード エディターを使って変更しないでください。
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            components = New System.ComponentModel.Container()
        End Sub

    End Class

```

### 注意事項
DockをFill、AnchorをTop,Left,Bottom,Rightに設定するとうまく動かないかもです。