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 a button's caption bold when the mouse moves over it
Keywordsbutton, caption, font, bold, mouse
CategoriesControls
 
Thanks to Vijay Sai Sharma

Make the button's caption bold in its MouseMove event handler. Make it non-bold in the Form's MouseMove event handler.

This example also keeps track of the button with the input focus. When the mouse moves over the form, it makes that button bold.

[Note that this method is not completely fool-proof. For example, if you move the mouse quickly enough, you may be able to get it off the form without firing any MouseMove events on the Form.]

 
'   This is the Button having Focus.
Dim cmdActiveButton As CommandButton

Private Sub Command1_GotFocus()
    '   Set Active Button to Command1
    Set cmdActiveButton = Me.Command1
    '   Make it's Font Bold.
    Call MakeButtonFontBold(Me.Command1)
End Sub

Private Sub Command1_LostFocus()
    '   Set Active Button to Nothing.
    Set cmdActiveButton = Nothing
    '   Make Button's Font Regular.
    Me.Command1.Font.Bold = False
End Sub

Private Sub Command1_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    '   Make Button's Font Bold.
    Call MakeButtonFontBold(Me.Command1)
End Sub

Private Sub Command2_GotFocus()
    '   Set Active Button to Command2.
    Set cmdActiveButton = Me.Command2
    '   Make it's Font Bold.
    Call MakeButtonFontBold(Me.Command2)
End Sub

Private Sub Command2_LostFocus()
    '   Set Active Button to Nothing.
    Set cmdActiveButton = Nothing
    '   Make Button's Font Regular.
    Me.Command2.Font.Bold = False
End Sub

Private Sub Command2_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    '   Make Button's Font Bold.
    Call MakeButtonFontBold(Me.Command2)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim Ctrl As Control

    If Not cmdActiveButton Is Nothing Then
        '   Make the Font Bold for the currently active
        ' button.
        Call MakeButtonFontBold(cmdActiveButton)
    Else
        '   Make the Font of all the buttons regular.
        For Each Ctrl In Me.Controls
            If TypeOf Ctrl Is CommandButton Then
                Ctrl.Font.Bold = False
            End If
        Next Ctrl
    End If
End Sub

Private Sub MakeButtonFontBold(ByRef cmdButtonToBold As _
    CommandButton)
Dim Ctrl As Control

    For Each Ctrl In Me.Controls
        If TypeOf Ctrl Is CommandButton Then
            If Ctrl.Name = cmdButtonToBold.Name Then
                Ctrl.Font.Bold = True
            Else
                Ctrl.Font.Bold = False
            End If
        End If
    Next Ctrl
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated