23

I went and created a tab containing a good amount of controls, most of which are contained within what I'll just call the top-level group box. Now I decide I'd like the text of the top-level group box to be bold, but nothing else. When I set the top-level group box's font to bold, however, all of the controls contained within it become bolded as well, which is what I don't want. I can set each individual control's bold property to false, but it seems like there should be an easier way to do this. Any ideas?

I'm probably missing something obvious, like a group box property that is staring me in the face--and apologize if this turns out to be the case.

Thanks in advance for any help.

RobC
  • 1,303
  • 3
  • 15
  • 32
  • 1
    Will an anchored panel inside the group box work? Whereby you have the font of the sub-panel as not bold and add all child controls added to the sub-panel instead. – Reddog Feb 17 '12 at 02:04
  • @Reddog: Yes, that's a better solution. I just noticed your comment after posting the same thing as an answer myself. – Cody Gray - on strike Feb 17 '12 at 02:52
  • No worries. I posted as a comment instead of an answer as I didn't know for sure if it would work... Just a hunch. Glad you found it to work though! – Reddog Feb 17 '12 at 18:36

5 Answers5

36

You could bypass the problem by placing a label over the caption for the GroupBox, but I wouldn't necessarily recommend this.

A better solution emerges once you understand what is happening and why it is happening. The issue is that a control's font (among other things) is an ambient property, meaning that child controls inherit their parent/container control's properties. So if you set the GroupBox to use a bold font, all of its child controls automatically inherit the bold property by default.

The key there is, of course, by default. Ambient properties only apply if you don't explicitly set the properties of the children to something else. If you don't want the child controls to be bold, select them all and turn off bold. The settings of the parent/container will no longer override the new custom settings.

To make things even easier, you can add a Panel control to your GroupBox, dock/anchor it to fill the entire client area of the GroupBox control, and set it to use a standard, non-bold font. Then, the rules of ambient controls stipulate that the child controls you add to the Panel will not be bold by default. This way, you only have to change the font property of one control as opposed to every child control that you add to the GroupBox.

The reason that this is better than trying to add a Label control over the GroupBox caption is because a GroupBox is designed to contain controls. You can take advantage of the docking and anchoring properties to make sure that everything gets arranged correctly, and you won't have to fight the designer when doing so to make sure that your custom Label correctly covers up the default label drawn by the GroupBox control. Additionally, you won't run into Z order issues or have other redrawing problems rear their ugly heads at runtime when, for example, the Label control gets accidentally hidden behind the GroupBox and no one can see it (and a host of other potential snafus).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks for the detailed explanation. I upped it and made it my accepted answer for that reason and because (like reddog's),it's the one I went with. Love this site. – RobC Feb 17 '12 at 03:12
  • Stellar explanation @Cody Gray. I truly thought I was looking for the quick-and-dirty solution of the separate label, if for no other reason than to use the minimum of my brain real estate. But your answer concisely showed that the better solution is worth the extra thought. – Arthur Hebert-Ryan May 14 '17 at 04:24
1

I came across this old question when searching for the same, and realised it could be solved in code without adding a separate control just to overcome the ambience issue that Code Gray mentions in his answer.

Add an 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 all the controls in all the GroupBoxes on a form (including any child GroupBoxes) by using as follows in the form's OnLoad event:

Me.Controls.UnBold(Of GroupBox)()

Or for all controls in a single GroupBox (again, including any child GroupBoxes):

MySpecificGroupBox.UnBold(Of GroupBox)()

With the proviso that if you actually want control within the GroupBox to actually stay emboldened you will have to set that in code after calling the extension.

Toby
  • 9,696
  • 16
  • 68
  • 132
1

Place all of your controls inside of a ContentControl and reset the font parameters

   <GroupBox Header="Group" FontSize="16" FontWeight="Bold">
       <ContentControl Margin="0" FontSize="12" FontWeight="Regular">
   ...
   ...
   ...
       </ContentControl>
   </GroupBox>
Michael A
  • 11
  • 2
0

Programatically you can do it in order. Assume you want to make font style bold in groupbox but not in child controls. First set the font to a new Font in child controls, in this case you can pass groupbox font property. Then change groupbox font style to bold.

        var grpBox = new GroupBox()
        {
            Text = "",
            Width = 780,
            Height = 70,
            Parent = panel1,
            Dock = DockStyle.None,
            AutoSize = false,
            Visible = true,
            Location = new Point(20, grpY)
        };
        var label = new Label()
        {
            AutoSize = true,
            Parent = grpBox,
            Enabled = true,
            Name = "label" + btnNum++,
            Location = new Point(5, 50),
            Text = "",
            Font = new Font(grpBox.Font, FontStyle.Regular)
        };

        var txtBox = new TextBox()
        {
            Width = 550,
            Height = 23,
            Location = new Point(65, 20),
            Name = "txtBox" + btnNum++,
            Parent = grpBox,
            Enabled = true,
            Tag = label,
            Font = new Font(grpBox.Font, FontStyle.Regular)
        };
        grpBox.Font = new Font(grpBox.Font, FontStyle.Bold);
ozanmut
  • 2,898
  • 26
  • 22
0

Consider bypassing the problem by placing a label over the GroupBox's text area and make the label's font bold.

I did it once and even used a CheckBox (for enabling/disabling the whole group). Worked like a charm.

AVIDeveloper
  • 2,954
  • 1
  • 25
  • 32
  • @reddog: Yours is good too, and the one I wish I'd come up with myself. (I'd like to think I would have, too, if I hadn't been spending so much (*&$&%* time on this project lately.) Your response just didn't catch my eye right off the bat. Thanks, though. – RobC Feb 17 '12 at 02:27