为什么这个标签的 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 时,相应组中的所有控件都会明显发生相应的变化,除了标签 。经过一个小时的故障排除,这是我所知道的:
- 标签 的
ForeColor
属性 会 改变,但不明显。
- 当我在循环外的标签上调用
EnableControl
时,标签明显发生变化。
- 无论我是专门将标签对象传递给子程序,还是从它的组集合中传递,都没有关系;在这两种情况下都可以看到变化
- 如果我在组切换事件中禁用了一个标签,然后专门针对该标签调用
EnableControl
以尝试再次禁用它,则没有明显的变化(可能是因为 ForeColor
属性 已经设置为 "off" 颜色)
- 在组切换事件 运行 时使用
Application.Echo
关闭屏幕更新,然后在事件结束时重新打开它并没有什么不同。
- 使用
For i = 1 to .Count
而不是 For Each
来创建群组切换事件 运行 并没有什么不同。
- 当更改不同的视觉效果时也会出现此问题 属性,例如 ForeTint。
- (根据评论)
Repaint
没有区别
- (根据评论)
DoEvents
没有区别
为什么会这样?
(第一个问题,如果我在 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
有效,因为它只影响标签,但不会禁用其关联的列表框。
我正在制作一个表单,其中包含我想 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 时,相应组中的所有控件都会明显发生相应的变化,除了标签 。经过一个小时的故障排除,这是我所知道的:
- 标签 的
ForeColor
属性 会 改变,但不明显。 - 当我在循环外的标签上调用
EnableControl
时,标签明显发生变化。- 无论我是专门将标签对象传递给子程序,还是从它的组集合中传递,都没有关系;在这两种情况下都可以看到变化
- 如果我在组切换事件中禁用了一个标签,然后专门针对该标签调用
EnableControl
以尝试再次禁用它,则没有明显的变化(可能是因为ForeColor
属性 已经设置为 "off" 颜色) - 在组切换事件 运行 时使用
Application.Echo
关闭屏幕更新,然后在事件结束时重新打开它并没有什么不同。 - 使用
For i = 1 to .Count
而不是For Each
来创建群组切换事件 运行 并没有什么不同。 - 当更改不同的视觉效果时也会出现此问题 属性,例如 ForeTint。
- (根据评论)
Repaint
没有区别 - (根据评论)
DoEvents
没有区别
为什么会这样?
(第一个问题,如果我在 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
有效,因为它只影响标签,但不会禁用其关联的列表框。