如何更改 GroupBox 文本的 ForeColor 而不更改其子 ForeColor? [英] How can I change the ForeColor of the GroupBox text without changing its children ForeColor?
问题描述
我想知道是否有一个选项可以更改窗口窗体中组框左上角的组框文本的颜色,而不是位于组框内的任何控件或标签.
I want to know if there is an option to just change the color of group box text at the top left of the group box in a windows form and not any controls or labels located inside of the group box.
我知道 GroupBox.ForeColor = Color.Blue
会将与该框关联的所有文本更改为蓝色,但它也会更改标签和其他控件的 ForeColor
GroupBox
.
I know that GroupBox.ForeColor = Color.Blue
will change all text associated with that box to blue, but it also changes the ForeColor
of labels and other controls in the GroupBox
.
如何更改分组框文本的颜色而不更改其子前景色?
How can I change the color of the group box text without changing its children forecolor?
推荐答案
ForeColor
属性是一个环境属性.环境属性是一个控件属性,如果未设置,则从父级检索控制.
The
ForeColor
property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control.
由于您没有为分组框中的标签和文本框设置 ForeColor
,它们将使用其父级的 ForeColor
值.您可以使用以下任一选项解决此问题:
Since you didn't set ForeColor
for the labels and textboxes in the group box, they will use ForeColor
value of their parent. You can solve this problem using either of these options:
在
GroupBox
中放置一个Panel
将GroupBox
的ForeColor
设置为Blue
并使用设计器将Panel
的ForeColor
显式设置为ControlText
.然后将其他控件放入Panel
.这样,您的控件将使用您明确设置的Panel
的ForeColor
.
Put a
Panel
inGroupBox
Set theForeColor
ofGroupBox
toBlue
and setForeColor
ofPanel
toControlText
explicitly using designer. Then put other controls in thePanel
. This way, your controls will useForeColor
ofPanel
which you set explicitly.
自定义GroupBox
的Paint
:
Private Sub GroupBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupBox1.Paint
e.Graphics.Clear(Me.GroupBox1.BackColor)
GroupBoxRenderer.DrawGroupBox(e.Graphics, Me.GroupBox1.ClientRectangle, _
Me.GroupBox1.Text, Me.GroupBox1.Font, Color.Blue, _
System.Windows.Forms.VisualStyles.GroupBoxState.Normal)
End Sub
这篇关于如何更改 GroupBox 文本的 ForeColor 而不更改其子 ForeColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!