为什么这个标签的 ForeColor 在循环中改变时不会明显改变?

Why won't the ForeColor of this label visibly change when altered in a loop?

我正在制作一个表单,其中包含我想 enable/disable 带有相关切换按钮(我们称它们为“组切换”)的控件组。每个组都有不同种类的控件类型,所以我做了一个通用的程序来处理切换:

'constants for control ForeColors
Public Enum LabelForeColor
    Default = 8355711
    Off = 14277081
End Enum

Public Enum ListForeColor
    Default = 4210752
    Off = 12566463
End Enum

Public Sub EnableControl(Ctrl As Control, Enabled As Boolean)
    With Ctrl
        Select Case Ctrl.ControlType
            Case acLabel
                If Enabled Then .ForeColor = LabelForeColor.Default Else .ForeColor = LabelForeColor.Off
                Debug.Print "LABEL", .ForeColor

            Case acListBox
                If Enabled Then .ForeColor = ListForeColor.Default Else .ForeColor = ListForeColor.Off
                .Enabled = Enabled
                Debug.Print "LIST", .ForeColor

            Case acCommandButton
                .Enabled = Enabled
                Debug.Print "BUTTON", "NA"

            Case acCheckBox
                .Enabled = Enabled
                Debug.Print "CHECK", "NA"

            Case Else
                Debug.Print "Control [" & .Name & "] is not of a type that EnableControl can handle."

        End Select
    End With
End Sub

每组控件由一个集合表示。加载表单时,每个带有特定标记 属性 的控件都会添加到相应的集合中。 Group Toggles 未添加到任何集合中,而是具有如下所示的事件过程:

Private Sub ToggleGroup1_AfterUpdate()
    Dim State As Boolean
    'a public function that converts the toggle button's value to a boolean
    State = FormCommon.ToggleButtonState(ToggleGroup1.Value)

    Dim iCtrl As Control
    For Each iCtrl In Controls_ByPlant
        FormCommon.EnableControl iCtrl, State
    Next iCtrl
End Sub

当我单击 GroupToggle 时,相应组中的所有控件都会明显发生相应的变化,除了标签 。经过一个小时的故障排除,这是我所知道的:

为什么会这样?

(第一个问题,如果我在 post 中搞砸了,我深表歉意)

这很有趣,但有点虎头蛇尾。

您的代码确实适用于标签,但实际情况是这样的:

  • 所有标签都与输入控件相关联(像往常一样)
  • 当你停用一个组时,你就禁用了输入控件(.Enabled = Enabled
  • 这会自动将关联的标签设置为(系统定义的)无法更改的浅灰色文本颜色。
  • 这个 "disabled label" 颜色与您的 LabelForeColor.Default 颜色非常相似,因此切换时很难看出变化。但它确实改变了。

更改颜色常量​​以使效果更明显:

Public Enum LabelForeColor
    Default = vbRed ' 8355711
    ' the "Off" color is never visible, unless you add an un-associated label to a group
    Off = vbBlue ' 14277081
End Enum

编辑:您的测试代码 FormCommon.EnableControl iCtrl, False 有效,因为它只影响标签,但不会禁用其关联的列表框。