Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake an extender provider that automatically displays status messages when the mouse is over controls in VB .NET
DescriptionThis example shows how to make an extender provider that automatically displays status messages when the mouse is over controls in VB .NET.
KeywordsVB .NET, extender provider, status, StatusBar, hover
CategoriesControls, VB.NET
 
The HoverStatus control inherits StatusBar and implements the IExtenderProvider interface.

The interface requires it to provide a CanExtend function that returns True if it is passed a type of control that the provider can extend. HoverStatus can extend all controls so it returns True if its parameter is a Control.

 
' We can extend any control.
Public Function CanExtend(ByVal extendee As Object) As _
    Boolean Implements _
    System.ComponentModel.IExtenderProvider.CanExtend
    Return TypeOf extendee Is Control
End Function
 
When the control is created, it adds a panel to itself (remember, it inherits StatusBar). This is the panel where the extender will later display status messages.
 
' Create the first panel (where we will show the status).
Public Sub New()
    MyBase.New()

    ' Make the first panel where we will display status.
    With Me.Panels.Add("")
        .AutoSize = StatusBarPanelAutoSize.Spring
        .Style = StatusBarPanelStyle.Text
    End With

    Me.ShowPanels = True
End Sub
 
The extender stores information about its client controls in ClientInfo objects. It keeps the objects in a hashtable named m_ClientInfo.

The extender provides a HoverTip property implemented by its GetHoverTip and SetHoverTip methods. GetHoverTip uses the GetClientInfo function to fetch a control's ClientInfo object from the hashtable and it returns the ClientInfo's HoverTip value.

Subroutine SetHoverTip uses GetClientInfo function to fetch a control's ClientInfo object from the hashtable. It updates the ClientInfo's HoverTip value and calls subroutine AddOrRemoveIfNecessary to update the hashtable appropriately.

 
' Stores information about our client controls.
Private m_ClientInfo As New Hashtable

' Information about a client control.
Private Class ClientInfo
    Public ClientControl As Control
    Public FocusTip As String = ""

    ' Save a reference to the client.
    Public Sub New(ByVal client_control As Control)
        ClientControl = client_control
    End Sub

    ' Return True if the control has a default blank
    ' FocusTip.
    Public Function IsDefault() As Boolean
        Return FocusTip = ""
    End Function
End Class

' Return this control's HoverTip.
<Category("Data"), _
 DefaultValue("")> _
Public Function GetHoverTip(ByVal client_control As _
    Control) As String
    ' Get the control's ClientInfo if it exists.
    Return GetClientInfo(client_control).HoverTip
End Function

' Set this control's HoverTip.
<Category("Data"), _
 DefaultValue("")> _
Public Sub SetHoverTip(ByVal client_control As Control, _
    ByVal tool_tip As String)
    ' Get this control's ClientInfo.
    Dim client_info As ClientInfo = _
        GetClientInfo(client_control)

    ' Set the new value.
    client_info.HoverTip = tool_tip

    ' Add or remove the ClientInfo if necessary.
    AddOrRemoveIfNecessary(client_control, client_info)
End Sub
 
Function GetClientInfo checks whether the hashtable contains a ClientInfo for a control. If the object is present, the function returns it. Otherwise it creates a new ClientInfo object with default values and returns it.

Subroutine AddOrRemoveIfNecessary checks whether a ClientInfo contains default data (a blank HoverTip value). If the value is not blank, then the routine ensures that the object is in the hashtable. If the value is blank, the routine ensures that the ClientInfo is not in the hashtable.

Subroutine AddOrRemoveIfNecessary also registers or unregisters an event handler to handle the client control's MouseEnter and MouseLeave events.

 
' Return this control's ClientInfo or a new blank one.
Private Function GetClientInfo(ByVal client_control As _
    Control) As ClientInfo
    ' See if we have ClientInfo for this control.
    If m_ClientInfo.Contains(client_control) Then
        ' We have ClientInfo for this control. Return it.
        Return DirectCast(m_ClientInfo(client_control), _
            ClientInfo)
    Else
        ' We do not have ClientInfo for this control.
        ' Return a new ClientInfo with a default blank
        ' string.
        Return New ClientInfo(client_control)
    End If
End Function

' Add or remove this ClientInfo if necessary.
Private Sub AddOrRemoveIfNecessary(ByVal client_control As _
    Control, ByVal client_info As ClientInfo)
    ' See if the ClientInfo should be present but is not,
    ' or should not be present but is.
    If client_info.IsDefault Then
        ' The ClientInfo should not be present.
        If m_ClientInfo.Contains(client_control) Then
            ' The ClientInfo is present but should not be.
            m_ClientInfo.Remove(client_control)
            RemoveHandler client_control.MouseEnter, _
                AddressOf Client_MouseEnter
            RemoveHandler client_control.MouseLeave, _
                AddressOf Client_MouseLeave
        End If
    Else
        ' The ClientInfo should be present.
        If Not m_ClientInfo.Contains(client_control) Then
            ' The ClientInfo is missing but should be
            ' present.
            m_ClientInfo.Add(client_control, client_info)
            AddHandler client_control.MouseEnter, AddressOf _
                Client_MouseEnter
            AddHandler client_control.MouseLeave, AddressOf _
                Client_MouseLeave
        End If
    End If
End Sub
 
When the mouse moves onto a client control (with a non-blank HoverTip value), the Client_MouseEnter event handler displays the control's HoverTip value in the status bar's first panel.

When the mouse moves off of a client control, the Client_MouseLeave event handler blanks the status bar's first panel.

 
' Handles client MouseEnter events.
Private Sub Client_MouseEnter(ByVal sender As Object, ByVal _
    e As System.EventArgs)
    ' Get the client's ClientInfo.
    Dim client_info As ClientInfo = GetClientInfo(sender)

    ' Show the focus tip.
    Me.Panels(0).Text = client_info.HoverTip
End Sub

' Handles client MouseLeave events.
Private Sub Client_MouseLeave(ByVal sender As Object, ByVal _
    e As System.EventArgs)
    ' Clear the focus tip.
    Me.Panels(0).Text = ""
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated