[ACCEPTED]-How do I make a group box's text bold but not the text of controls contained in it?-containers
You could bypass the problem by placing a label 44 over the caption for the GroupBox, but I 43 wouldn't necessarily recommend this.
A better solution 42 emerges once you understand what is happening 41 and why it is happening. The issue is that 40 a control's font (among other things) is 39 an ambient property, meaning that child controls 38 inherit their parent/container control's 37 properties. So if you set the GroupBox to 36 use a bold font, all of its child controls 35 automatically inherit the bold property 34 by default.
The key there is, of course, by 33 default. Ambient properties only apply if 32 you don't explicitly set the properties 31 of the children to something else. If you 30 don't want the child controls to be bold, select 29 them all and turn off bold. The settings 28 of the parent/container will no longer override 27 the new custom settings.
To make things even 26 easier, you can add a Panel control to your 25 GroupBox, dock/anchor it to fill the entire 24 client area of the GroupBox control, and 23 set it to use a standard, non-bold font. Then, the 22 rules of ambient controls stipulate that 21 the child controls you add to the Panel 20 will not be bold by default. This way, you 19 only have to change the font property of 18 one control as opposed to every child control 17 that you add to the GroupBox.
The reason 16 that this is better than trying to add a 15 Label control over the GroupBox caption 14 is because a GroupBox is designed to contain 13 controls. You can take advantage of the 12 docking and anchoring properties to make 11 sure that everything gets arranged correctly, and 10 you won't have to fight the designer when 9 doing so to make sure that your custom Label 8 correctly covers up the default label drawn 7 by the GroupBox control. Additionally, you 6 won't run into Z order issues or have other 5 redrawing problems rear their ugly heads 4 at runtime when, for example, the Label 3 control gets accidentally hidden behind the GroupBox 2 and no one can see it (and a host of other 1 potential snafus).
I came across this old question when searching 13 for the same, and realised it could be solved 12 in code without adding a separate control 11 just to overcome the ambience issue that 10 Code Gray mentions in his answer.
Add an 9 extensions in a module like so:
<Extension()> Public Sub UnBold(Of T As Control)(cc As Control.ControlCollection) For Each c As Control In cc If Not TypeOf c Is T AndAlso c.GetType.GetProperty("Font") IsNot Nothing Then Dim RegularFont As New Font(c.Font.FontFamily, c.Font.Size, FontStyle.Regular) c.Font = RegularFont ElseIf c.HasChildren Then UnBold(Of T)(c.Controls) End If Next End Sub
Then unbold 8 all the controls in all the
GroupBoxes on a form 7 (including any child
GroupBoxes) by using as follows 6 in the form's
Or for all controls 5 in a single
GroupBox (again, including any child 4
With the proviso that if you actually 3 want control within the
GroupBox to actually stay emboldened 2 you will have to set that in code after 1 calling the extension.
Consider bypassing the problem by placing 4 a label over the GroupBox's text area and 3 make the label's font bold.
I did it once 2 and even used a CheckBox (for enabling/disabling 1 the whole group). Worked like a charm.
Place all of your controls inside of a ContentControl 1 and reset the font parameters
<GroupBox Header="Group" FontSize="16" FontWeight="Bold"> <ContentControl Margin="0" FontSize="12" FontWeight="Regular"> ... ... ... </ContentControl> </GroupBox>
More Related questions