I'm developing Windows Forms application and have a GroupBox
with many Labels and TextBoxes. If I change the GroupBox's font then the Label's and TextBox's font are also changes. Is there a way to change the GroupBox's font without change Label's and TextBox's font in it?
Asked
Active
Viewed 4,924 times
2

Reza Aghaei
- 120,393
- 18
- 203
- 398

Willy
- 1,689
- 7
- 36
- 79
-
1Here is a similar question about `ForeColor` of `GroupBox`: [How can I change the ForeColor of the GroupBox text without changing its children ForeColor?](http://stackoverflow.com/a/37793912/3110834) – Reza Aghaei Aug 30 '16 at 10:57
-
You can use either of those 2 options in the linked post, using the panel approach or using the paint event. The paint event has less side-effects and can be simply applied using code. – Reza Aghaei Aug 30 '16 at 11:08
4 Answers
1
You need to set them, otherwise they will be inherited. So just change the font of the labels inside.

ThePerplexedOne
- 2,920
- 15
- 30
-
The [`Font`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.font(v=vs.110).aspx) property is an ambient property. – Reza Aghaei Aug 30 '16 at 10:51
-
1An ambient property is a property on a control that, if not set, is retrieved from the parent control. If the control does not have a parent and the property is not set, the control tries to find the value of the ambient property through the Site property. If the control is not sited, the site does not support ambient properties, or the property is not set on the AmbientProperties object, the Control uses its own default values. Some objects derived from the Control class might set the property even if you do not. – Reza Aghaei Aug 30 '16 at 10:51
-
1I have a lot of Forms, I'm looking for another way rather than set the Label or TextBox's font individually – Willy Aug 30 '16 at 10:52
-
Well if your font isn't consistent throughout your application, there's not much you can really do other than changing them yourself within the designer. – ThePerplexedOne Aug 30 '16 at 10:54
-
@RezaAghaei, there are so many Details Form, which is used to display the row information. All of the controls used the default, and I asked to change all of the GroupBox's font to distinguish it with row data. In other way GroupBox Text is just like the header. – Willy Aug 30 '16 at 10:59
-
-
@RezaAghaei I think this is what I want, but it's a little bit tricky I think, because we used the panel to manipulate the font, but it's ok, thanks – Willy Aug 30 '16 at 11:04
-
@Willy You can select all of the controls in the group box and change their font at once. – Slai Aug 30 '16 at 12:15
1
You can change the Font of the controls at run-time to the Font of the group box parent:
InitializeComponent();
foreach(Control c in this.groupBox1.Controls)
c.Font = c.Parent.Parent.Font;
For few group boxes:
foreach(Control p in new[] {groupBox1, groupBox2})
foreach (Control c in p.Controls)
c.Font = c.Parent.Parent.Font; // or just p.Parent.Font;

Slai
- 22,144
- 5
- 45
- 53
0
I often dock a Panel to Fill inside the GroupBox. Then I change the Panel Font to what I want. Then I add all the labels and other controls. They will inherit from the Panel rather than the GroupBox.

John Kurtz
- 775
- 8
- 16
-1
In Design mode you can go to View > Other Windows > Document Outline
Now you can notice the controls are under the group box and inherit the group box font.
In the designer, you can move the controls a bit out of the group box and then you can move the control with the arrow keys so that it is above the group box but not in it.
[

Slai
- 22,144
- 5
- 45
- 53
-
2If you move controls above the `GroupBox` you loose the ability to disable them all by setting `GroupBox.Enabled` property. This way, you don't need to have a `GroupBox` at all and you can simply draw a `GroupBox` around contents which is not so useful. – Reza Aghaei Aug 30 '16 at 11:17
-
This causes the container to lose all control over the items within it, and renders it merely a box on the page. Problems include, what if you move it somewhere else on the page, what if you disable it or hide it. Not a good idea. – LordWilmore Aug 30 '16 at 11:22
-
1I rather keep this answer anyway because it might be a tiny bit useful for people that don't know about the Document Outline window. – Slai Aug 30 '16 at 11:48