TakeFocusOnClick 属性示例

下例用 TakeFocusOnClick 属性,控制单击命令按钮时,该命令按钮是否接受焦点。用户先单击一个除 CommandButton1 之外的其他控件,然后再单击 CommandButton1,如果 TakeFocusOnClick 是 True,在单击 CommandButton1 后,CommandButton1 将接受焦点。用户可以通过单击切换按钮来改变 TakeFocusOnClick 的值。

窗体包含:

  • 名为 CommandButton1 的命令按钮。
  • 名为 ToggleButton1 的切换按钮。
  • 一至二个其他控件,如选项按钮或列表框。
Sub UserForm_CommandButton1_Click()
    MsgBox "Watch CommandButton1 to see if it takes the focus."
End Sub

Sub UserForm_ToggleButton1_Click()
    If UserForm_ToggleButton1 = True Then
        UserForm_CommandButton1.TakeFocusOnClick = True
        UserForm_ToggleButton1.Caption = "TakeFocusOnClick On"
    Else
        UserForm_CommandButton1.TakeFocusOnClick = False
        UserForm_ToggleButton1.Caption = "TakeFocusOnClick Off"
    End If
End Sub

Sub UserForm_Initialize()
    UserForm_CommandButton1.Caption = "Show Message"
    
    UserForm_ToggleButton1.Caption = "TakeFocusOnClick On"
    UserForm_ToggleButton1.Value = True
    UserForm_ToggleButton1.Width = 90
End Sub

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22