Results 1 to 11 of 11

Thread: Collapse/expand Panel Custom Control

  1. #1

    Thread Starter
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Question Collapse/expand Panel Custom Control

    I am trying to add the panel control the functionality of expanding and collapsing. My panel has a header, where the user should place a label with the title or an icon, and a body, where you can place controls as you do in standard panel. Both header and body have customizable gradient background.

    That works, but then I have added a timer to the custom control to expand or collapse the body of the panel. It seems to work fine at design time, but at runtime the panel do not raise events. Even the controls you place on the panel appear as not enabled. I know the problem is the declaration of the timer "withevents". Is there a way to make it work including the timer in the custom control?

    Thanks!

  2. #2

    Thread Starter
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: Collapse/expand Panel Custom Control

    Here is the full code:

    VB Code:
    1. Imports System.Drawing
    2. Imports System.Drawing.Drawing2D
    3. Imports System.ComponentModel
    4.  
    5. Public Class zsPanelHeader
    6.     Inherits Panel
    7.     Friend WithEvents Timer1 As System.Windows.Forms.Timer
    8.     Private components As System.ComponentModel.IContainer
    9.  
    10.     Public Enum Curve As Integer
    11.         Triangle
    12.         SigmaBell
    13.     End Enum
    14.     Public Enum GradientType As Integer
    15.         SideToSide
    16.         InOut
    17.     End Enum
    18.  
    19.     Private Sub MyGradient(ByVal gr As Graphics)
    20.         If Me.Width = 0 Or Me.Height = 0 Then Exit Sub
    21.         Dim zsRectangulo As Rectangle
    22.         ' Linear Gradient
    23.         If zsGradientType = GradientType.SideToSide Then
    24.             If MinHeight > 0 Then 'Header
    25.                 zsRectangulo = New Rectangle(0, 0, Me.Width, zsMinHeight)
    26.                 Dim zsbrocha As New LinearGradientBrush(zsRectangulo, _
    27.                 ColorHeader1, ColorHeader2, Gradient)
    28.                 If CurveGradient = Curve.SigmaBell Then
    29.                     zsbrocha.SetSigmaBellShape(Blend)
    30.                 Else
    31.                     zsbrocha.SetBlendTriangularShape(Blend)
    32.                 End If
    33.                 gr.FillRectangle(zsbrocha, zsRectangulo)
    34.                 zsbrocha.Dispose()
    35.             End If
    36.             If Me.Height > zsMinHeight Then ' Body
    37.                 zsRectangulo = New Rectangle(0, MinHeight, Me.Width, _
    38.                 Me.Height - zsMinHeight)
    39.                 Dim zsbrocha As New LinearGradientBrush(zsRectangulo, _
    40.                 Color1, Color2, Gradient)
    41.                 If CurveGradient = Curve.SigmaBell Then
    42.                     zsbrocha.SetSigmaBellShape(Blend)
    43.                 Else
    44.                     zsbrocha.SetBlendTriangularShape(Blend)
    45.                 End If
    46.                 gr.FillRectangle(zsbrocha, zsRectangulo)
    47.                 zsbrocha.Dispose()
    48.             End If
    49.             ' In-Out Gradient
    50.         Else
    51.             If MinHeight > 0 Then 'Header
    52.                 Dim pnt() As Point = {New Point(0, 0), New Point(Me.Width, 0), _
    53.                  New Point(Me.Width, zsMinHeight), New Point(0, zsMinHeight)}
    54.                 Dim zsbrocha As New PathGradientBrush(pnt)
    55.                 zsbrocha.CenterColor = ColorHeader1
    56.                 zsbrocha.SurroundColors = New Color() {ColorHeader2}
    57.                 If CurveGradient = Curve.SigmaBell Then
    58.                     zsbrocha.SetSigmaBellShape(Blend)
    59.                 Else
    60.                     zsbrocha.SetBlendTriangularShape(Blend)
    61.                 End If
    62.                 gr.FillPolygon(zsbrocha, pnt)
    63.                 zsbrocha.Dispose()
    64.             End If
    65.             If Me.Height > zsMinHeight Then 'Body
    66.                 Dim pnt() As Point = {New Point(0, zsMinHeight), _
    67.                  New Point(Me.Width, zsMinHeight), _
    68.                  New Point(Me.Width, zsMaxHeight), New Point(0, zsMaxHeight)}
    69.                 Dim zsbrocha As New PathGradientBrush(pnt)
    70.                 zsbrocha.CenterColor = Color1
    71.                 zsbrocha.SurroundColors = New Color() {Color2}
    72.                 If CurveGradient = Curve.SigmaBell Then
    73.                     zsbrocha.SetSigmaBellShape(Blend)
    74.                 Else
    75.                     zsbrocha.SetBlendTriangularShape(Blend)
    76.                 End If
    77.                 gr.FillPolygon(zsbrocha, pnt)
    78.                 zsbrocha.Dispose()
    79.             End If
    80.  
    81.         End If
    82.         zsRectangulo = Nothing
    83.         gr.Dispose()
    84.     End Sub
    85.  
    86.     Dim zsColor1 As Color = Color.RoyalBlue
    87.     <Description("First Gradient Color"), Category("Gradient")> _
    88.     Public Property Color1() As Color
    89.         Get
    90.             Color1 = zsColor1
    91.         End Get
    92.         Set(ByVal value As Color)
    93.             zsColor1 = value
    94.             Me.Refresh()
    95.         End Set
    96.     End Property
    97.  
    98.     Dim zsColor2 As Color = Color.LightSkyBlue
    99.     <Description("Second Gradient Color"), Category("Gradient")> _
    100.     Public Property Color2() As Color
    101.         Get
    102.             Color2 = zsColor2
    103.         End Get
    104.         Set(ByVal value As Color)
    105.             zsColor2 = value
    106.             Me.Refresh()
    107.         End Set
    108.     End Property
    109.  
    110.     Dim zsColorHeader1 As Color = Color.Maroon
    111.     <Description("First Header Gradient Color"), Category("Gradient")> _
    112.     Public Property ColorHeader1() As Color
    113.         Get
    114.             ColorHeader1 = zsColorHeader1
    115.         End Get
    116.         Set(ByVal value As Color)
    117.             zsColorHeader1 = value
    118.             Me.Refresh()
    119.         End Set
    120.     End Property
    121.  
    122.     Dim zsColorHeader2 As Color = Color.Gold
    123.     <Description("Second Header Gradient Color"), Category("Gradient")> _
    124.     Public Property ColorHeader2() As Color
    125.         Get
    126.             ColorHeader2 = zsColorHeader2
    127.         End Get
    128.         Set(ByVal value As Color)
    129.             zsColorHeader2 = value
    130.             Me.Refresh()
    131.         End Set
    132.     End Property
    133.  
    134.     Dim zsGradientMode As LinearGradientMode = LinearGradientMode.Vertical
    135.     <Description("Gradient Mode"), Category("Gradient")> _
    136.     Public Property Gradient() As LinearGradientMode
    137.         Get
    138.             Gradient = zsGradientMode
    139.         End Get
    140.         Set(ByVal value As LinearGradientMode)
    141.             zsGradientMode = value
    142.             Me.Refresh()
    143.         End Set
    144.     End Property
    145.  
    146.     Dim zsBlend As Single = 0.5
    147.     <Description("Single number [0 to 1] indicating blend center position"), _
    148.     Category("Gradient")> _
    149.     Public Property Blend() As Single
    150.         Get
    151.             Blend = zsBlend
    152.         End Get
    153.         Set(ByVal value As Single)
    154.             If value > 1 Then value = 1
    155.             If value < 0 Then value = 0
    156.             zsBlend = value
    157.             Me.Refresh()
    158.         End Set
    159.     End Property
    160.  
    161.     Dim zsCurve As Curve = Curve.Triangle
    162.     <Description("Curve"), Category("Gradient")> _
    163.     Public Property CurveGradient() As Curve
    164.         Get
    165.             CurveGradient = zsCurve
    166.         End Get
    167.         Set(ByVal value As Curve)
    168.             zsCurve = value
    169.             Me.Refresh()
    170.         End Set
    171.     End Property
    172.  
    173.     Dim zsGradientType As GradientType = GradientType.SideToSide
    174.     <Description("Gradient direction"), Category("Gradient")> _
    175.     Public Property Direction() As GradientType
    176.         Get
    177.             Direction = zsGradientType
    178.         End Get
    179.         Set(ByVal value As GradientType)
    180.             zsGradientType = value
    181.             Me.Refresh()
    182.         End Set
    183.     End Property
    184.  
    185.  
    186.     Dim zsMinHeight As Integer = 30
    187.     <Description("Height of collapsed panel"), Category("Collapse")> _
    188. Public Property MinHeight() As Integer
    189.         Get
    190.             MinHeight = zsMinHeight
    191.         End Get
    192.         Set(ByVal value As Integer)
    193.             zsMinHeight = value
    194.         End Set
    195.     End Property
    196.     Dim zsMaxHeight As Integer = 100
    197.     <Description("Height of expanded panel"), Category("Collapse")> _
    198. Public Property MaxHeight() As Integer
    199.         Get
    200.             MaxHeight = zsMaxHeight
    201.         End Get
    202.         Set(ByVal value As Integer)
    203.             zsMaxHeight = value
    204.         End Set
    205.     End Property
    206.     Dim zsSpeed As Integer = 10
    207.     <Description("Collapsing/Expanding Speed"), Category("Collapse")> _
    208. Public Property Speed() As Integer
    209.         Get
    210.             Speed = zsSpeed
    211.         End Get
    212.         Set(ByVal value As Integer)
    213.             If value < 1 Then value = 1
    214.             If value > zsMaxHeight - zsMinHeight Then value = zsMaxHeight - zsMinHeight
    215.             zsSpeed = value
    216.  
    217.         End Set
    218.     End Property
    219.     Dim zsCollapsed As Boolean
    220.     Dim inc As Integer = 1
    221.     <Description("Indicates whether the panel is collapsed"), Category("Collapse")> _
    222. Public Property Collapsed() As Boolean
    223.         Get
    224.             Collapsed = zsCollapsed
    225.         End Get
    226.         Set(ByVal value As Boolean)
    227.             If value <> zsCollapsed Then
    228.                 zsCollapsed = value
    229.                 StartAction()
    230.             End If
    231.  
    232.         End Set
    233.     End Property
    234.  
    235.     Private Sub Gradient_Paint(ByVal sender As Object, _
    236.     ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    237.         MyGradient(e.Graphics)
    238.     End Sub
    239.  
    240.     Public Sub StartAction()
    241.         If Me.Height <= zsMinHeight Then inc = 1 Else inc = -1
    242.         Timer1.Start()
    243.         Resizing = True
    244.         RaiseEvent ActionStarted()
    245.     End Sub
    246.  
    247.     Public Event ActionStarted()
    248.     Public Event ActionStopped()
    249.     Public Event ActionTick()
    250.     Public Resizing As Boolean = False
    251.  
    252.     Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    253.     Handles Timer1.Tick
    254.         If inc < 0 And Me.Height <= zsMinHeight Then
    255.             Timer1.Stop()
    256.             Me.Height = zsMinHeight
    257.             RaiseEvent ActionStopped()
    258.             Resizing = False
    259.         ElseIf inc > 0 And Me.Height >= zsMaxHeight Then
    260.             Timer1.Stop()
    261.             Me.Height = zsMaxHeight
    262.             RaiseEvent ActionStopped()
    263.             Resizing = False
    264.         Else
    265.             Me.Height += inc * zsSpeed
    266.             RaiseEvent ActionTick()
    267.         End If
    268.     End Sub
    269.  
    270.     Public Sub New() ' CONSTRUCTOR
    271.         Me.components = New System.ComponentModel.Container
    272.         Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    273.         Me.Timer1.Interval = 10
    274.         Me.Enabled = False
    275.         Me.SuspendLayout()
    276.         Me.ResumeLayout(False)
    277.     End Sub
    278.  
    279.     Private Sub zsPanel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) _
    280.     Handles Me.Resize
    281.         If Me.DesignMode And Not Resizing Then
    282.             If zsCollapsed Then
    283.                 zsMinHeight = Me.Height
    284.                 If Me.Height > zsMaxHeight Then Me.Height = zsMaxHeight
    285.             Else
    286.                 zsMaxHeight = Me.Height
    287.                 If Me.Height < zsMinHeight Then Me.Height = zsMinHeight
    288.             End If
    289.         End If
    290.         Me.Refresh()
    291.     End Sub
    292. End Class

  3. #3
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: Collapse/expand Panel Custom Control

    May be is caused by this method:

    VB Code:
    1. Public Sub New() ' CONSTRUCTOR
    2.         Me.components = New System.ComponentModel.Container
    3.         Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    4.         Me.Timer1.Interval = 10
    5.         Me.Enabled = True
    6.         Me.SuspendLayout()
    7.         Me.ResumeLayout(False)
    8.     End Sub

  4. #4

    Thread Starter
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: Collapse/expand Panel Custom Control

    mmmmm... What is wrong with that constructor lingsn?
    I inicialize Timer1 as a new component for the custom control, and asign values to properties interval and enabled. The fact is that timer works fine, but is the only thing that works. I am sure there must be a simple solution but I just can not find it. Any clues?

  5. #5
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: Collapse/expand Panel Custom Control

    The constructor had set the panel to disabled (me.Enabled = False). That's why the textbox you put in the panel will become disabled.

  6. #6

    Thread Starter
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: Collapse/expand Panel Custom Control

    Thanks lingsn, that's true. But still the controls and the panel itself do not raise events. For example, I am trying to handle double click for the panel, in order to make it collapse/expand. The event handler never run.

  7. #7
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: Collapse/expand Panel Custom Control

    Just a suggestion, add this to your zsPanelHeader to see whether the result is what you want.

    VB Code:
    1. Private Sub zsPanel_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
    2.         Me.Collapsed = Not Me.Collapsed
    3.     End Sub

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Collapse/expand Panel Custom Control

    You just need to use WithEvents when declaring your szPanelHeader object.

  9. #9

    Thread Starter
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: Collapse/expand Panel Custom Control

    This class corresponds to a user control. While testing it I amb having two projects in the solution, the zspanel, and a windows application with just a form to contain the new control. I do not declare the zspanelheader, I just build the zspanelheader control, and drag the new component from the toolbox to the form as I would do with any other control.

    I have the same control without the timer and it works perfectly. But as soon as I place a timer inside the component the controls inside appear as not enabled and the panel and components inside do not raise events. I have no clue on what's happening.

  10. #10

    Thread Starter
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: Collapse/expand Panel Custom Control

    When I run the application with the zspanel on it, it keeps showing its contents as not enabled. I modified the mistaken me.enabled=false for me.timer1.enabled=false. But as you can see on the picture attached the result is the same.
    Attached Images Attached Images  

  11. #11
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: Collapse/expand Panel Custom Control

    Is your zspanel developed as a separate project that will compile to dll and later add as a reference to other project? If yes then sometimes you have to delete the reference in the project and add it back again in order for the changes to take effect.

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