How can I change the ForeColor of the GroupBox text without changing its children ForeColor?
我想知道是否有一个选项可以仅更改Windows窗体中组框左上方的组框文本的颜色,而不是位于组框内的任何控件或标签。
我知道
如何更改组框文本的颜色而不更改其子原色?
The
ForeColor property is an ambient property. An ambient property is
a control property that, if not set, is retrieved from the parent
control.
由于您没有在组框中设置标签和文本框的
在
自定义
1 2 3 4 5 6 7 8 | 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之后更改它们。 您可以对每对控件/颜色使用字典。
就像是:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Dim cColors As New Dictionary(Of Control, Color) For Each ctrl As Control In GroupBox1.Controls cColors.Add(ctrl, ctrl.ForeColor) Next GroupBox1.ForeColor = Color.Blue For Each ctrl As Control In GroupBox1.Controls If cColors.HasKey(ctrl) Then ctrl.ForeColor = cColors(ctrl) End If Next |
您可以将其放入方法中。
有关在MSDN上的属性的更多信息。