如何更改 GroupBox 文本的 ForeColor 而不更改其子 ForeColor? [英] How can I change the ForeColor of the GroupBox text without changing its children ForeColor?

查看:40
本文介绍了如何更改 GroupBox 文本的 ForeColor 而不更改其子 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 会将与该框关联的所有文本更改为蓝色,但它也会更改标签和其他控件的 ForeColorGroupBox.

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:

  1. GroupBox中放置一个PanelGroupBoxForeColor设置为Blue 并使用设计器将 PanelForeColor 显式设置为 ControlText.然后将其他控件放入Panel.这样,您的控件将使用您明确设置的 PanelForeColor.

  1. Put a Panel in GroupBox Set the ForeColor of GroupBox to Blue and set ForeColor of Panel to ControlText explicitly using designer. Then put other controls in the Panel. This way, your controls will use ForeColor of Panel which you set explicitly.

自定义GroupBoxPaint:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆