Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I style a GroupBox caption separately from its contents?

I'm developing a WinForms application and on one form, I use a GroupBox to group related controls together. I would like to style the caption in a particular way (e.g. making the caption bold). By altering the styling for the GroupBox, I can customise the caption, but it also alters the styling for controls within the GroupBox.

So far, there aren't that many child controls, so I'm individually resetting their styles back to the default, but I figure there has to be a better way... but I haven't figured it out so far.

Is there a way?

like image 269
CJM Avatar asked Nov 24 '10 15:11

CJM


2 Answers

The easiest way would be to put another panel inside the GroupBox, so that you only have to reset the font once.

There are plenty of other solutions, but they all require a lot more code. This keeps it nice and simple.

like image 136
Scott Rippey Avatar answered Oct 15 '22 22:10

Scott Rippey


Control.Font (and other style-related properties) is implemented in such a way that it asks its parent for Font if it does not have explicitly set one. Obviously, this means that you would have to set it individually to all of the child controls to make sure they are not affected by changes in parent style.

Maybe you should take a look at owner-drawing the GroupBox caption?

like image 39
olygofren Avatar answered Oct 15 '22 21:10

olygofren