TripleState 属性和开关控件示例

下例用 TripleState 属性,使 Null 可作为复选框和切换按钮的一个合法值。用户通过 ToggleButton2 控制 TripleState 的值。用户可根据 TripleState 的值来设置复选框或切换按钮的值。

窗体包含 :

  • 名为 CheckBox1 的复选框。
  • 名为 ToggleButton1 的切换按钮。
  • 名为 ToggleButton2 的切换按钮。
Sub UserForm_Initialize()
    UserForm_CheckBox1.Caption = "Value is True"
    UserForm_CheckBox1.Value = True
    UserForm_CheckBox1.TripleState = False
    
    UserForm_ToggleButton1.Caption = "Value is True"
    UserForm_ToggleButton1.Value = True
    UserForm_ToggleButton1.TripleState = False

    UserForm_ToggleButton2.Value = False
    UserForm_ToggleButton2.Caption = "Triple State Off"
End Sub

Sub UserForm_ToggleButton2_Click()
    If UserForm_ToggleButton2.Value = True Then
        UserForm_ToggleButton2.Caption = "Triple State On"
        UserForm_CheckBox1.TripleState = True
        UserForm_ToggleButton1.TripleState = True
    Else
        UserForm_ToggleButton2.Caption = "Triple State Off"
        UserForm_CheckBox1.TripleState = False
        UserForm_ToggleButton1.TripleState = False
    End If
End Sub

Sub UserForm_CheckBox1_Change()
    If IsNull(UserForm_CheckBox1.Value) Then
        UserForm_CheckBox1.Caption = "Value is Null"
    ElseIf UserForm_CheckBox1.Value = False Then
        UserForm_CheckBox1.Caption = "Value is False"
    ElseIf UserForm_CheckBox1.Value = True Then
        UserForm_CheckBox1.Caption = "Value is True"
    End If
End Sub

Sub UserForm_ToggleButton1_Change()
    If IsNull(UserForm_ToggleButton1.Value) Then
        UserForm_ToggleButton1.Caption = "Value is Null"
    ElseIf UserForm_ToggleButton1.Value = False Then
        UserForm_ToggleButton1.Caption = "Value is False"
    ElseIf UserForm_ToggleButton1.Value = True Then
        UserForm_ToggleButton1.Caption = "Value is True"
    End If
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44