Results 1 to 6 of 6

Thread: RollOverPanel and RollOverLabel

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    RollOverPanel and RollOverLabel

    I wrote a simple tutorial on a RollOverLabel I while back. One of the reasons I created the label was for an application I am building that has a menu like so:



    I wanted the heading and description to change colour when I entered the "outer" region. I also wanted to be able to click anywhere in this region and preform one click event. What I came up with was the reworked RollOverLabel and the new RollOverPanel.

    Wheter they know it or not the following people had a part to play and have my thanks for helping complete this project.

    Athiest, Max Mayhofer, Jmcilhinney and Deepak Sakpal.

    Panel Code
    VB Code:
    1. 'Component Name: RollOverPanel
    2. 'Author: Dean McDonnell
    3. 'Date: 29 Sept 2008
    4. Imports System.ComponentModel
    5.  
    6. <ToolboxBitmap(GetType(Panel))> _
    7. Public Class RollOverPanel
    8.     Inherits System.Windows.Forms.Panel
    9.  
    10.  
    11. #Region "Overriden Methods."
    12.     ''' <summary>
    13.     ''' OnMouseEnter - Calls ActivateHotControls.
    14.     ''' </summary>
    15.     Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
    16.         MyBase.OnMouseEnter(e)
    17.         ActivateHotControls()
    18.     End Sub
    19.  
    20.     ''' <summary>
    21.     ''' OnMouseLeave - Deactivates Hot Controls. This method does not know if the mouse entered
    22.     ''' a child control. This is handled by the MouseInChild() Method.
    23.     ''' </summary>
    24.     Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
    25.         MyBase.OnMouseLeave(e)
    26.         DeactivateHotControls()
    27.     End Sub
    28.  
    29.     ''' <summary>
    30.     ''' OnControlAdded - Adds handlers to each control as they are created via the AddHandleToChildren(e)
    31.     ''' method. They are destroyed in the finalize method"
    32.     ''' </summary>
    33.     Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
    34.         MyBase.OnControlAdded(e)
    35.         AddHandleToChildren(e)
    36.     End Sub
    37.  
    38.     ''' <summary>
    39.     ''' Finalize - Overrided to remove handle from children.
    40.     ''' </summary>
    41.     Protected Overrides Sub Finalize()
    42.         MyBase.Finalize()
    43.         RemoveHandleFromChildren()
    44.     End Sub
    45. #End Region
    46.  
    47. #Region "Custom Methods."
    48.     ''' <summary>
    49.     ''' AddHandleToChildren - Places handles on each child to allow the panel to control when its hot and if it
    50.     ''' is clicked. If these where not in place when the user moused over other controls in the panel the
    51.     ''' RollOverLabels would revert to their base color. The second handler allows the user to click anywhere
    52.     ''' on the panel including its children. Clicks are handled by Instance.OnClick().
    53.     ''' </summary>
    54.     Private Sub AddHandleToChildren(ByVal e As System.Windows.Forms.ControlEventArgs)
    55.         AddHandler e.Control.MouseEnter, AddressOf MouseInChild
    56.         AddHandler e.Control.Click, AddressOf ChildClicked
    57.     End Sub
    58.  
    59.     ''' <summary>
    60.     ''' RemoveHandleFromChildren - See above for explaination on why handles where added. This method
    61.     ''' simply removes them.
    62.     ''' </summary>
    63.     Private Sub RemoveHandleFromChildren()
    64.         For Each Child As Control In Me.Controls
    65.             RemoveHandler Child.MouseEnter, AddressOf MouseInChild
    66.             RemoveHandler Child.Click, AddressOf ChildClicked
    67.         Next
    68.     End Sub
    69.  
    70.     ''' <summary>
    71.     ''' ChildClicked - Allows the user of the RollOverPanel to call just one Click() Method to handle the
    72.     ''' panel and its children. Each child control that is clicked will fire this event.
    73.     ''' </summary>
    74.     Private Sub ChildClicked(ByVal sender As Object, ByVal e As System.EventArgs)
    75.         Me.OnClick(e)
    76.     End Sub
    77.  
    78.     ''' <summary>
    79.     ''' MouseInChild - This method enables the hot controls to stay hot even if mouseleave is fired by the
    80.     ''' panel because the mouse entered a child control.
    81.     ''' </summary>
    82.     Private Sub MouseInChild(ByVal sender As Object, ByVal e As System.EventArgs)
    83.         ActivateHotControls()
    84.     End Sub
    85.  
    86.     ''' <summary>
    87.     ''' DeactivateHotControls - Loops through each RollOver Control and calls the triggeroff() method
    88.     ''' of said controls. TriggerOff() reverts from the controls "hot" color to its base color.
    89.     ''' </summary>
    90.     Private Sub DeactivateHotControls()
    91.         For Each Control In Me.Controls
    92.             If TypeOf Control Is RollOverLabel Then
    93.                 Dim CurrentLabel As RollOverLabel = CType(Control, RollOverLabel)
    94.                 CurrentLabel.TriggerOff()
    95.             End If
    96.         Next
    97.     End Sub
    98.  
    99.     ''' <summary>
    100.     ''' ActivateHotControls - Loops through each RollOver control and calls the triggeroff() method of said
    101.     ''' controls. TriggerOn() changes the controls base color to its "hot" color.
    102.     ''' </summary>
    103.     Private Sub ActivateHotControls()
    104.         For Each Control In Me.Controls
    105.             If TypeOf Control Is RollOverLabel Then
    106.                 Dim CurrentLabel As RollOverLabel
    107.                 CurrentLabel = CType(Control, RollOverLabel)
    108.                 CurrentLabel.TriggerOn()
    109.             End If
    110.         Next
    111.     End Sub
    112. #End Region
    113.  
    114. End Class

    Label Code
    VB Code:
    1. 'Component Name: RollOverLabel
    2. 'Author: Dean McDonnell
    3. 'Date: 29 Sept 2008
    4. Imports System.ComponentModel
    5.  
    6. <ToolboxBitmap(GetType(Label))> _
    7. Public Class RollOverLabel
    8.     Inherits System.Windows.Forms.Label
    9.  
    10. #Region "Properties and Their Variables."
    11.  
    12.     ''' <summary>
    13.     ''' BaseColor = ForeColor     HotColor = RollOverColor
    14.     ''' </summary>
    15.     Private BaseColor As Color = Color.Black
    16.     Private HotColor As Color = Color.Black
    17.  
    18.     ''' <summary>
    19.     ''' RollOverColor - A simple property to GET/SET the RollOver color.
    20.     ''' </summary>
    21.     <Description("Sets the color the label will change to when moused over")> _
    22.     <Category("Appearance")> _
    23.     Public Property RollOverColor() As Color
    24.         Get
    25.             Return HotColor
    26.         End Get
    27.  
    28.         Set(ByVal value As Color)
    29.             HotColor = value
    30.         End Set
    31.     End Property
    32. #End Region
    33.  
    34. #Region "Overriden Methods."
    35.     ''' <summary>
    36.     ''' OnMouseEnter - Calls TriggerOn() if a RollOverPanel is not its parent.
    37.     ''' </summary>
    38.     Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
    39.         MyBase.OnMouseEnter(e)
    40.  
    41.         If TypeOf Me.Parent Is RollOverPanel Then
    42.             Exit Sub
    43.         Else
    44.             TriggerOn()
    45.         End If
    46.     End Sub
    47.  
    48.     ''' <summary>
    49.     ''' OnMouseLeave - Calls TriggerOf() if a RollOverPanel is not its parent.
    50.     ''' </summary>
    51.     Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
    52.         MyBase.OnMouseLeave(e)
    53.  
    54.         If TypeOf Me.Parent Is RollOverPanel Then
    55.             Exit Sub
    56.         Else
    57.             TriggerOff()
    58.         End If
    59.     End Sub
    60. #End Region
    61.  
    62. #Region "Custom Methods."
    63.     ''' <summary>
    64.     ''' This method changes the foreColor from the base color to the hot color.
    65.     ''' </summary>
    66.     ''' <remarks>This method is visible to the RollOverPanel because of the Friend Modifier</remarks>
    67.     Friend Sub TriggerOn()
    68.         If Me.ForeColor = Me.HotColor Then
    69.             Exit Sub
    70.         Else
    71.             BaseColor = Me.ForeColor
    72.             Me.ForeColor = HotColor
    73.         End If
    74.  
    75.     End Sub
    76.  
    77.     ''' <summary>
    78.     ''' This method changes the foreColor from the hot color to the base color.
    79.     ''' </summary>
    80.     ''' <remarks>This method is visible to the RollOverPanel because of the Friend Modifer</remarks>
    81.     Friend Sub TriggerOff()
    82.         Me.ForeColor = BaseColor
    83.     End Sub
    84. #End Region
    85.  
    86. End Class

    Any problems or if you think you can refactor any particular part better let me know let me know.
    Last edited by DeanMc; Sep 29th, 2008 at 04:40 PM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: RollOverPanel and RollOverLabel

    Guys,

    I teach myself VB and as such I have nobody to look over my code and help me to refactor it. If you find anything wrong please DO post. While I wrote the original code I have no qualms with refactoring it to make to make the code cleaner and run better. I have received one submission already and will change the code shortly.

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: RollOverPanel and RollOverLabel

    Instead of removing all eventhandlers and adding them again when OnControlAdded is called, you should make use of the ControlEventArgs object and add that single control only:
    VB.NET Code:
    1. Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
    2.         MyBase.OnControlAdded(e)
    3.         AddHandler e.Control.MouseEnter, AddressOf MouseInChild
    4.             AddHandler e.Control.Click, AddressOf ChildClicked
    5.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: RollOverPanel and RollOverLabel

    Thanks Athiest, this is the change I was talking about. Im currently updating the source and will post it again soon.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: RollOverPanel and RollOverLabel

    Right that looks much better and will save a lot of looping. Ive kept the AddHandler() method though for cleanlyness sake.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: RollOverPanel and RollOverLabel

    I just thought this was worth mentioning. a panels child controls CANNOT sit on the border of the panel control. If they do the mouseleave event wont fire and the control will not work:



    As you can see my picture boxes are 1px away from the border so it works now. And just a screen of them in action:


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