TakeFocusOnClick Property

Applies To

CommandButton control.

Description

Specifies whether a control takes the focus when clicked.

Syntax

object.TakeFocusOnClick [= Boolean]

The TakeFocusOnClick property syntax has these parts:

Part

Description

object

Required. A valid object.

Boolean

Optional. Specifies whether a control takes the focus when clicked.


Settings

The settings for Boolean are:

Value

Description

True

The button takes the focus when clicked (default).

False

The button does not take the focus when clicked.


Remarks

The TakeFocusOnClick property defines only what happens when the user clicks a control. If the user tabs to the control, the control takes the focus regardless of the value of TakeFocusOnClick.

Use this property to complete actions that affect a control without requiring that control to give up focus. For example, assume your form includes a TextBox and a CommandButton that checks for correct spelling of text. You would like to be able to select text in the TextBox, then click the CommandButton and run the spelling checker without taking focus away from the TextBox. You can do this by setting the TakeFocusOnClick property of the CommandButton to False.

See Also

TabStop property.

Example

The following example uses the TakeFocusOnClick property to control whether a CommandButton receives the focus when the user clicks on it. The user clicks a control other than CommandButton1 and then clicks CommandButton1. If TakeFocusOnClick is True, CommandButton1 receives the focus after it is clicked. The user can change the value of TakeFocusOnClick by clicking the ToggleButton.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • A CommandButton named CommandButton1.
  • A ToggleButton named ToggleButton1.
  • One or two other controls, such as an OptionButton or ListBox.
    Private Sub CommandButton1_Click()
        MsgBox "Watch CommandButton1 to see if it takes the focus."
    End Sub
    
    Private Sub ToggleButton1_Click()
        If ToggleButton1 = True Then
            CommandButton1.TakeFocusOnClick = True
            ToggleButton1.Caption = "TakeFocusOnClick On"
        Else
            CommandButton1.TakeFocusOnClick = False
            ToggleButton1.Caption = "TakeFocusOnClick Off"
        End If
    End Sub
    
    Private Sub UserForm_Initialize()
        CommandButton1.Caption = "Show Message"
        
        ToggleButton1.Caption = "TakeFocusOnClick On"
        ToggleButton1.Value = True
        ToggleButton1.Width = 90
    End Sub