AdvancedGroupBox
A group box with a styled frame: a gradient or solid body, a solid, dotted or rounded border, and a
header band with its own background, border and font – a drop-in replacement for
GroupBox.
public partial class AdvancedGroupBox : GroupBox
Inherits from / implements: GroupBox
Remarks
What it is for
Use it wherever a form groups related fields – "Delivery address", "Payment", "Account
settings" – and the stock group box looks too plain. It is a GroupBox, so it keeps the
container behaviour, the tab order and the designer support; only the drawing is replaced.
The parts of the frame
- Body – filled from half-way down the header to the bottom, with a gradient
from
BackgroundBrushColor1toBackgroundBrushColor2atLinearGradientBrushGradientAngle, or withControl.BackColorwhenIsBackgroundDrawingUsingLinerGradientBrushisfalse. - Border –
BorderColoratBorderThickness, drawn as a line or as dashes (ControlBorderStyle), optionally with rounded corners (ShouldDrawRoundedCorners,RoundedCornerTargets,RoundedCornersAngle). - Header – the control's
Control.TextinHeaderFontandHeaderForeColor, on a band with its own gradient or solid background and border, placed byHeaderAlignment.
Good to know
- While the body is a gradient, a child control added with the group box's own
Control.BackColoris given a transparent background, so labels and check boxes do not paint solid bands across the gradient. A child whose back colour was set to something else is left alone. - Metrics – margins, thicknesses, dash lengths, corner size – are in logical pixels and are scaled for the display the control is on.
- The header and the body read
HeaderFont, notControl.Font;Control.Fontis still the font children inherit.
Examples
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.Containers;
public class CheckoutForm : Form
{
public CheckoutForm()
{
Text = "Checkout";
ClientSize = new Size(380, 220);
var deliveryGroup = new AdvancedGroupBox
{
Text = "Delivery address",
Location = new Point(12, 12),
Size = new Size(356, 150),
HeaderFont = new Font("Segoe UI", 9.75f, FontStyle.Bold),
HeaderForeColor = Color.White,
HeaderBackgroundColor1 = Color.SteelBlue,
HeaderBackgroundColor2 = Color.MidnightBlue,
HeaderBorderColor = Color.MidnightBlue,
BackgroundBrushColor1 = Color.White,
BackgroundBrushColor2 = Color.AliceBlue,
BorderColor = Color.SteelBlue,
ShouldDrawRoundedCorners = true,
RoundedCornerTargets = GroupBoxCornerTargets.BottomLeft | GroupBoxCornerTargets.BottomRight,
RoundedCornersAngle = 16
};
deliveryGroup.Controls.Add(new Label { Text = "Postcode", Location = new Point(16, 40), AutoSize = true });
deliveryGroup.Controls.Add(new TextBox { Location = new Point(100, 36), Width = 120 });
deliveryGroup.Controls.Add(new CheckBox { Text = "Same as billing address", Location = new Point(16, 76), AutoSize = true });
Controls.Add(deliveryGroup);
}
}See also
Members at a glance
| Constructors | |
|---|---|
AdvancedGroupBox | Initialises a group box with a top-left header on a white-to-dark-grey gradient band with a black
border, a dark grey one-pixel frame, and a body shaded from SystemColors.Control to
SystemColors.ControlDark. |
| Properties | |
BackgroundBrushColor1 | Gets or sets the colour the body gradient starts with – the top, at the default angle. |
BackgroundBrushColor2 | Gets or sets the colour the body gradient ends with – the bottom, at the default angle. |
BorderColor | Gets or sets the colour of the frame round the body. |
BorderThickness | Gets or sets the width of the frame round the body. |
ControlBorderStyle | Gets or sets whether the frame is drawn as a continuous line or as dashes. |
DottedBorderDepth | Gets or sets the length of each dash in a dotted frame. |
DottedBorderGap | Gets or sets the space between the dashes of a dotted frame. |
HeaderAlignment | Gets or sets where along the top edge the header sits. |
HeaderBackgroundColor1 | Gets or sets the colour the header band's gradient starts with. |
HeaderBackgroundColor2 | Gets or sets the colour the header band's gradient ends with. |
HeaderBackgroundLeftMargin | Gets or sets extra space added to the header band on the left of the text, on top of
TextLeftMargin. |
HeaderBackgroundLinearGradientBrushGradientAngle | Gets or sets the direction of the header band's gradient. |
HeaderBackgroundRightMargin | Gets or sets extra space added to the header band on the right of the text, on top of
TextRightMargin. |
HeaderBorderColor | Gets or sets the colour of the border round the header band. |
HeaderBorderThickness | Gets or sets the width of the border round the header band. |
HeaderExtraHeightFactor | Gets or sets how much taller than its text the header band is. |
HeaderFont | Gets or sets the font of the header text. |
HeaderForeColor | Gets or sets the colour of the header text. |
HeaderSolidBrushBackgroundColor | Gets or sets the colour of the header band when it is filled with a solid colour. |
IsBackgroundDrawingUsingLinerGradientBrush | Gets or sets whether the body is filled with a gradient rather than a solid colour. |
IsHeaderBackgroundDrawingEnabled | Gets or sets whether the header band has a background of its own. |
IsHeaderBackgroundGradient | Gets or sets whether the header band is filled with a gradient rather than a solid colour. |
IsHeaderBorderDrawingEnabled | Gets or sets whether a border is drawn round the header band. |
LinearGradientBrushGradientAngle | Gets or sets the direction of the body gradient. |
RoundedCornerTargets | Gets or sets which corners of the frame are rounded. |
RoundedCornersAngle | Gets or sets how round the rounded corners are – the size of each corner's curve, despite the name not an angle. |
ShouldDrawRoundedCorners | Gets or sets whether the frame's corners are rounded. |
ShouldUpdateChildrenUponParentFormMoveSize | Gets or sets whether the group box repaints itself each time the form it is on moves or is resized. |
TextLeftMargin | Gets or sets the space between the left edge of the header band and the header text. |
TextRightMargin | Gets or sets the space between the header text and the right edge of the header band. |
| Methods | |
OnControlAdded | Raises the Control.ControlAdded event and, while the body is a gradient, gives the new
child a transparent background if it has the group box's own back colour. |
OnHandleCreated | Raises the Control.HandleCreated event and starts listening to the form's move and
resize, for ShouldUpdateChildrenUponParentFormMoveSize. |
OnHandleDestroyed | Stops listening to the form and raises the Control.HandleDestroyed event. |
OnPaint | Raises the Control.Paint event and then draws the body, the frame and the header over
the whole control. |
OnParentChanged | Raises the Control.ParentChanged event and, once the window exists, moves the move and
resize listening to the form the group box is now on. |
Constructors
AdvancedGroupBox
Initialises a group box with a top-left header on a white-to-dark-grey gradient band with a black
border, a dark grey one-pixel frame, and a body shaded from SystemColors.Control to
SystemColors.ControlDark.
Properties
BackgroundBrushColor1
Gets or sets the colour the body gradient starts with – the top, at the default angle.
- Value
- The start colour. The default is
SystemColors.Control.
Remarks
Used only while IsBackgroundDrawingUsingLinerGradientBrush is
true.
See also
BackgroundBrushColor2
Gets or sets the colour the body gradient ends with – the bottom, at the default angle.
- Value
- The end colour. The default is
SystemColors.ControlDark.
See also
BorderColor
Gets or sets the colour of the frame round the body.
- Value
- The frame colour. The default is
Color.DarkGray.
See also
BorderThickness
Gets or sets the width of the frame round the body.
- Value
- The width in logical pixels. The default is 1. Zero or less draws no frame.
ControlBorderStyle
Gets or sets whether the frame is drawn as a continuous line or as dashes.
- Value
- One of the
GroupBoxBorderStylevalues. The default isGroupBoxBorderStyle.Normal.
Remarks
Rounded corners (ShouldDrawRoundedCorners) apply only to
GroupBoxBorderStyle.Normal; a dotted frame is always square. The dashes are sized by
DottedBorderDepth and DottedBorderGap.
DottedBorderDepth
Gets or sets the length of each dash in a dotted frame.
- Value
- The dash length in logical pixels. The default is 4.
Remarks
Used only while ControlBorderStyle is
GroupBoxBorderStyle.Dotted.
See also
DottedBorderGap
Gets or sets the space between the dashes of a dotted frame.
- Value
- The gap in logical pixels. The default is 2.
See also
HeaderAlignment
Gets or sets where along the top edge the header sits.
- Value
- One of the
GroupBoxHeaderAlignmentvalues. The default isGroupBoxHeaderAlignment.TopLeft.
Remarks
With GroupBoxHeaderAlignment.Custom, the header starts
Control.Padding.Padding.Left pixels from the left edge – at least 1, and
no further than leaves room for the whole header. The control's padding also sets the space round its
children, as it does on any container.
Example
paymentGroup.HeaderAlignment = GroupBoxHeaderAlignment.Custom;
paymentGroup.Padding = new Padding(40, 0, 0, 0); // header 40 pixels in from the leftHeaderBackgroundColor1
Gets or sets the colour the header band's gradient starts with.
- Value
- The start colour. The default is
Color.White.
See also
HeaderBackgroundColor2
Gets or sets the colour the header band's gradient ends with.
- Value
- The end colour. The default is
Color.DarkGray.
See also
HeaderBackgroundLeftMargin
Gets or sets extra space added to the header band on the left of the text, on top of
TextLeftMargin.
- Value
- The space in logical pixels. The default is 0.
Remarks
The text moves right by this amount and the band widens by it. At display scales above 100% the band widens by the unscaled amount while the text moves by the scaled one, so large values can push the end of the text towards the band's edge.
See also
HeaderBackgroundLinearGradientBrushGradientAngle
Gets or sets the direction of the header band's gradient.
- Value
- The angle in degrees, measured clockwise from the x-axis. The default is 90: top to bottom.
See also
HeaderBackgroundRightMargin
Gets or sets extra space added to the header band on the right of the text, on top of
TextRightMargin.
- Value
- The space in logical pixels. The default is 0.
See also
HeaderBorderColor
Gets or sets the colour of the border round the header band.
- Value
- The border colour. The default is
Color.Black.
See also
HeaderBorderThickness
Gets or sets the width of the border round the header band.
- Value
- The width in logical pixels. The default is 1. Zero or less draws no header border.
HeaderExtraHeightFactor
Gets or sets how much taller than its text the header band is.
- Value
- The extra height in logical pixels, shared above and below the text. The default is 8.
HeaderFont
Gets or sets the font of the header text.
- Value
- The header font. The default is the control's
Control.Fontat the time it was created.
Remarks
Changing Control.Font afterwards does not change the header; it is still the
font the group box's children inherit. The header band grows to the height of this font plus
HeaderExtraHeightFactor.
HeaderForeColor
Gets or sets the colour of the header text.
- Value
- The text colour. The default is the control's
Control.ForeColorat the time it was created – normallySystemColors.ControlText.
HeaderSolidBrushBackgroundColor
Gets or sets the colour of the header band when it is filled with a solid colour.
- Value
- The band colour. The default is
Color.Empty, which fills nothing, so the frame line shows through the band until a colour is set.
Remarks
Used when IsHeaderBackgroundDrawingEnabled is true and
IsHeaderBackgroundGradient is false.
IsBackgroundDrawingUsingLinerGradientBrush
Gets or sets whether the body is filled with a gradient rather than a solid colour.
- Value
truefor a gradient fromBackgroundBrushColor1toBackgroundBrushColor2;falseto fill withControl.BackColor. The default istrue.
Remarks
Setting it to true gives every child that has the group box's own back
colour a transparent background, so the gradient shows through. Setting it back to
false does not restore those children's colours.
See also
IsHeaderBackgroundDrawingEnabled
Gets or sets whether the header band has a background of its own.
- Value
trueto fill the band with a gradient or a solid colour (seeIsHeaderBackgroundGradient);falseto set the header text straight on the frame, which is broken round it. The default istrue.
Example
// A classic look: plain caption text in the gap of a thin frame.
accountGroup.IsHeaderBackgroundDrawingEnabled = false;
accountGroup.IsHeaderBorderDrawingEnabled = false;
accountGroup.IsBackgroundDrawingUsingLinerGradientBrush = false;IsHeaderBackgroundGradient
Gets or sets whether the header band is filled with a gradient rather than a solid colour.
- Value
truefor a gradient fromHeaderBackgroundColor1toHeaderBackgroundColor2;falseto fill withHeaderSolidBrushBackgroundColor. The default istrue.
Remarks
Has an effect only while IsHeaderBackgroundDrawingEnabled is
true.
IsHeaderBorderDrawingEnabled
Gets or sets whether a border is drawn round the header band.
- Value
trueto outline the header inHeaderBorderColor. The default istrue.
See also
LinearGradientBrushGradientAngle
Gets or sets the direction of the body gradient.
- Value
- The angle in degrees, measured clockwise from the x-axis. The default is 90: top to bottom.
Remarks
0 runs left to right, 90 top to bottom, 45 diagonally from the top-left corner.
RoundedCornerTargets
Gets or sets which corners of the frame are rounded.
- Value
- A combination of
GroupBoxCornerTargetsflags. The default isGroupBoxCornerTargets.All.
Remarks
Used only while ShouldDrawRoundedCorners is true.
Example
summaryGroup.ShouldDrawRoundedCorners = true;
summaryGroup.RoundedCornerTargets = GroupBoxCornerTargets.TopRight | GroupBoxCornerTargets.BottomLeft;RoundedCornersAngle
Gets or sets how round the rounded corners are – the size of each corner's curve, despite the name not an angle.
- Value
- The diameter of the corner arc in logical pixels. The default is 60. It is limited to the width and height of the body; zero or less gives square corners.
See also
ShouldDrawRoundedCorners
Gets or sets whether the frame's corners are rounded.
- Value
trueto round the corners named byRoundedCornerTargets. The default isfalse.
Remarks
Applies only while ControlBorderStyle is
GroupBoxBorderStyle.Normal. The body is clipped to the rounded shape, and the area
outside it shows the colour of the nearest parent that is not transparent. With rounded corners the
top line runs behind the header band rather than stopping at it.
See also
ShouldUpdateChildrenUponParentFormMoveSize
Gets or sets whether the group box repaints itself each time the form it is on moves or is resized.
- Value
trueto repaint on every move and resize of the form;falseto repaint only when Windows asks. The default isfalse.
Remarks
Turn it on if the group box sits over something that changes as the form moves – a transparent parent showing a background image, say – and is left with stale drawing. Only the group box itself is invalidated, not its children.
TextLeftMargin
Gets or sets the space between the left edge of the header band and the header text.
- Value
- The space in logical pixels. The default is 3.
Remarks
The header band grows to hold this margin, TextRightMargin and the text.
See also
TextRightMargin
Gets or sets the space between the header text and the right edge of the header band.
- Value
- The space in logical pixels. The default is 3.
See also
Methods
OnControlAdded
Raises the Control.ControlAdded event and, while the body is a gradient, gives the new
child a transparent background if it has the group box's own back colour.
Parameters
e- The child that was added.
Remarks
A child that cannot take a transparent background keeps its colour.
OnHandleCreated
Raises the Control.HandleCreated event and starts listening to the form's move and
resize, for ShouldUpdateChildrenUponParentFormMoveSize.
Parameters
e- The data for the event.
OnHandleDestroyed
Stops listening to the form and raises the Control.HandleDestroyed event.
Parameters
e- The data for the event.
OnPaint
Raises the Control.Paint event and then draws the body, the frame and the header over
the whole control.
Parameters
e- The graphics surface and the area to paint.
Remarks
The control's drawing covers everything a Control.Paint handler drew, because
the handlers run first. To draw on top of the group box, override this method and draw after calling
the base implementation.
OnParentChanged
Raises the Control.ParentChanged event and, once the window exists, moves the move and
resize listening to the form the group box is now on.
Parameters
e- The data for the event.