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 controls receive the focus in VB .NET
DescriptionThis example shows how to make an extender provider that automatically displays status messages when controls receive the focus in VB .NET.
KeywordsVB .NET, extender provider, status, StatusBar, focus
CategoriesControls, VB.NET
 
The FocusStatus 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. FocusStatus 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 FocusTip property implemented by its GetFocusTip and SetFocusTip methods. GetFocusTip uses the GetClientInfo function to fetch a control's ClientInfo object from the hashtable and it returns the ClientInfo's FocusTip value.

Subroutine SetFocusTip uses GetClientInfo function to fetch a control's ClientInfo object from the hashtable. It updates the ClientInfo's FocusTip 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 FocusTip.
<Category("Data"), _
 DefaultValue("")> _
Public Function GetFocusTip(ByVal client_control As _
    Control) As String
    ' Get the control's ClientInfo if it exists.
    Return GetClientInfo(client_control).FocusTip
End Function

' Set this control's FocusTip.
<Category("Data"), _
 DefaultValue("")> _
Public Sub SetFocusTip(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.FocusTip = 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 FocusTip 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 GotFocus and LostFocus 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.GotFocus, _
                AddressOf Client_GotFocus
            RemoveHandler client_control.LostFocus, _
                AddressOf Client_LostFocus
        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.GotFocus, AddressOf _
                Client_GotFocus
            AddHandler client_control.LostFocus, AddressOf _
                Client_LostFocus
        End If
    End If
End Sub
 
When a client control (with a non-blank FocusTip value) receives the input focus, the Client_GotFocus event handler displays the control's FocusTip value in the status bar's first panel.

When a client control loses the input focus, the Client_LostFocus event handler blanks the status bar's first panel.

 
' Handles client GotFocus events.
Private Sub Client_GotFocus(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.FocusTip
End Sub

' Handles client LostFocus events.
Private Sub Client_LostFocus(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