'Component Name: RollOverPanel
'Author: Dean McDonnell
'Date: 29 Sept 2008
Imports System.ComponentModel
<ToolboxBitmap(GetType(Panel))> _
Public Class RollOverPanel
Inherits System.Windows.Forms.Panel
#Region "Overriden Methods."
''' <summary>
''' OnMouseEnter - Calls ActivateHotControls.
''' </summary>
Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
ActivateHotControls()
End Sub
''' <summary>
''' OnMouseLeave - Deactivates Hot Controls. This method does not know if the mouse entered
''' a child control. This is handled by the MouseInChild() Method.
''' </summary>
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
DeactivateHotControls()
End Sub
''' <summary>
''' OnControlAdded - Adds handlers to each control as they are created via the AddHandleToChildren(e)
''' method. They are destroyed in the finalize method"
''' </summary>
Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
MyBase.OnControlAdded(e)
AddHandleToChildren(e)
End Sub
''' <summary>
''' Finalize - Overrided to remove handle from children.
''' </summary>
Protected Overrides Sub Finalize()
MyBase.Finalize()
RemoveHandleFromChildren()
End Sub
#End Region
#Region "Custom Methods."
''' <summary>
''' AddHandleToChildren - Places handles on each child to allow the panel to control when its hot and if it
''' is clicked. If these where not in place when the user moused over other controls in the panel the
''' RollOverLabels would revert to their base color. The second handler allows the user to click anywhere
''' on the panel including its children. Clicks are handled by Instance.OnClick().
''' </summary>
Private Sub AddHandleToChildren(ByVal e As System.Windows.Forms.ControlEventArgs)
AddHandler e.Control.MouseEnter, AddressOf MouseInChild
AddHandler e.Control.Click, AddressOf ChildClicked
End Sub
''' <summary>
''' RemoveHandleFromChildren - See above for explaination on why handles where added. This method
''' simply removes them.
''' </summary>
Private Sub RemoveHandleFromChildren()
For Each Child As Control In Me.Controls
RemoveHandler Child.MouseEnter, AddressOf MouseInChild
RemoveHandler Child.Click, AddressOf ChildClicked
Next
End Sub
''' <summary>
''' ChildClicked - Allows the user of the RollOverPanel to call just one Click() Method to handle the
''' panel and its children. Each child control that is clicked will fire this event.
''' </summary>
Private Sub ChildClicked(ByVal sender As Object, ByVal e As System.EventArgs)
Me.OnClick(e)
End Sub
''' <summary>
''' MouseInChild - This method enables the hot controls to stay hot even if mouseleave is fired by the
''' panel because the mouse entered a child control.
''' </summary>
Private Sub MouseInChild(ByVal sender As Object, ByVal e As System.EventArgs)
ActivateHotControls()
End Sub
''' <summary>
''' DeactivateHotControls - Loops through each RollOver Control and calls the triggeroff() method
''' of said controls. TriggerOff() reverts from the controls "hot" color to its base color.
''' </summary>
Private Sub DeactivateHotControls()
For Each Control In Me.Controls
If TypeOf Control Is RollOverLabel Then
Dim CurrentLabel As RollOverLabel = CType(Control, RollOverLabel)
CurrentLabel.TriggerOff()
End If
Next
End Sub
''' <summary>
''' ActivateHotControls - Loops through each RollOver control and calls the triggeroff() method of said
''' controls. TriggerOn() changes the controls base color to its "hot" color.
''' </summary>
Private Sub ActivateHotControls()
For Each Control In Me.Controls
If TypeOf Control Is RollOverLabel Then
Dim CurrentLabel As RollOverLabel
CurrentLabel = CType(Control, RollOverLabel)
CurrentLabel.TriggerOn()
End If
Next
End Sub
#End Region
End Class