BinaryNumericUpDown
A numeric up-down box with a flat, single-line border and spin buttons in colours you choose – for quantities, prices, percentages and any other number the user types or steps through.
public class BinaryNumericUpDown : NumericUpDown
Inherits from / implements: NumericUpDown
Remarks
What it is for
A drop-in replacement for NumericUpDown: everything the standard control does – the range,
the increment, decimal places, thousands separators, hexadecimal, keyboard and accessibility – works
unchanged. What it adds is the look: a flat border that lights up in BorderColor, and spin
buttons drawn in three states – resting, hot (under the pointer or holding the focus) and pressed.
Appearance
DrawStyleIsFlatswitches the flat drawing on (the default) or off, giving back the standard 3D look.ShowBorderAlwayskeeps the border and the buttons in their hot colours at all times (the default). Set it tofalsefor the classic hover effect, where the control rests borderless and lights up under the pointer or with the focus.UpDownButtonNormalBackColor,UpDownButtonOnFocusBackColorandUpDownArrowPressedBackColorcolour the buttons in the three states, andUpDownArrowsColorthe arrows.
The button colours follow the Windows colour scheme until they are set. The spin buttons widen with the font, so a large font or a high display scale keeps them in proportion to the text.
Examples
A quantity box on an order form that rests borderless, lights up in steel blue, and updates the order total as it changes.
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.InputControls;
const decimal unitPrice = 4.75m;
var totalLabel = new Label { Location = new Point(150, 19), AutoSize = true };
var quantity = new BinaryNumericUpDown
{
Location = new Point(16, 16),
Width = 120,
Minimum = 1,
Maximum = 500,
Value = 12,
ShowBorderAlways = false,
BorderColor = Color.SteelBlue,
UpDownButtonOnFocusBackColor = Color.AliceBlue,
UpDownArrowPressedBackColor = Color.LightSteelBlue,
UpDownArrowsColor = Color.DarkSlateBlue
};
quantity.ValueChanged += (sender, e) =>
totalLabel.Text = (quantity.Value * unitPrice).ToString("C");
orderForm.Controls.Add(quantity);
orderForm.Controls.Add(totalLabel);Members at a glance
| Constructors | |
|---|---|
BinaryNumericUpDown | Initialises the control with flat drawing on, the border shown at all times and the button colours taken from the Windows colour scheme. |
| Properties | |
BorderColor | Gets or sets the colour of the control's one-pixel border while it is hot, and of the outline round hot spin buttons. |
BorderStyle | Gets or sets the border style, which this control keeps at BorderStyle.Fixed3D. |
DrawStyleIsFlat | Gets or sets whether the control is drawn flat, with the colours set on it, or with the standard 3D look. |
ShowBorderAlways | Gets or sets whether the border and spin buttons are shown in their hot colours at all times, rather than only while the pointer is over the control or it has the focus. |
UpDownArrowPressedBackColor | Gets or sets the colour a spin button is filled with while it is held down. |
UpDownArrowsColor | Gets or sets the colour of the up and down arrows on the spin buttons. |
UpDownButtonNormalBackColor | Gets or sets the colour the spin buttons are filled with at rest – when the pointer is elsewhere and the control does not have the focus. |
UpDownButtonOnFocusBackColor | Gets or sets the colour the spin buttons are filled with while they are hot: while the pointer is over the
control, while it has the focus, and at all times while ShowBorderAlways is set. |
| Methods | |
CreateHandle | Creates the control's window. |
OnFontChanged | Raises the Control.FontChanged event, then fits the spin button to the new font. |
OnGotFocus | Raises the Control.GotFocus event and draws the border and buttons in their hot colours. |
OnLayout | Raises the Control.Layout event, then fits the spin button to the control's height and font. |
OnLostFocus | Raises the Control.LostFocus event and, unless ShowBorderAlways is set,
returns the border and buttons to their resting colours. |
OnSystemColorsChanged | Raises the Control.SystemColorsChanged event when the Windows colour scheme changes. |
WndProc | Processes the window's messages, drawing the flat border while DrawStyleIsFlat is set. |
Constructors
BinaryNumericUpDown
Initialises the control with flat drawing on, the border shown at all times and the button colours taken from the Windows colour scheme.
Properties
BorderColor
Gets or sets the colour of the control's one-pixel border while it is hot, and of the outline round hot spin buttons.
- Value
- The default is
SystemColors.Highlight.
Remarks
The border is hot while the pointer is over the control, while it has the focus, or always when
ShowBorderAlways is set. At rest it is drawn in Control.BackColor, so it disappears.
BorderStyle
Gets or sets the border style, which this control keeps at BorderStyle.Fixed3D.
- Value
- Always
BorderStyle.Fixed3D: any other value is ignored. The flat drawing paints over the 3D border, soDrawStyleIsFlatis the setting that changes the look.
DrawStyleIsFlat
Gets or sets whether the control is drawn flat, with the colours set on it, or with the standard 3D look.
- Value
truefor the flat look. The default istrue.- Default
true
Remarks
While it is false none of the colour properties is used and the control looks
exactly like NumericUpDown. Changing it re-creates the control's window.
ShowBorderAlways
Gets or sets whether the border and spin buttons are shown in their hot colours at all times, rather than only while the pointer is over the control or it has the focus.
- Value
trueto show them always. The default istrue.
Remarks
While it is true the border is drawn in BorderColor and the buttons in
UpDownButtonOnFocusBackColor whatever the pointer does, so
UpDownButtonNormalBackColor is not seen. Set it to false for a control that
rests with a border the colour of its background and lights up on hover or focus – the usual choice for a
dense form or a toolbar.
A disabled control always draws its border in SystemColors.ControlDark.
See also
UpDownArrowPressedBackColor
Gets or sets the colour a spin button is filled with while it is held down.
- Value
- The default is a strong shade of the Windows highlight colour, which follows the colour scheme. Set
Color.Emptyto return to it.
Remarks
Only the button under the pointer – up or down – takes this colour; the other keeps its hot colour.
See also
UpDownArrowsColor
Gets or sets the colour of the up and down arrows on the spin buttons.
- Value
- The default is
SystemColors.ControlText.
Remarks
The arrows keep this colour when the control is disabled.
UpDownButtonNormalBackColor
Gets or sets the colour the spin buttons are filled with at rest – when the pointer is elsewhere and the control does not have the focus.
- Value
- The default is
SystemColors.Control, following the colour scheme. SetColor.Emptyto return to it.
Remarks
Seen only while ShowBorderAlways is false, and on a disabled control.
UpDownButtonOnFocusBackColor
Gets or sets the colour the spin buttons are filled with while they are hot: while the pointer is over the
control, while it has the focus, and at all times while ShowBorderAlways is set.
- Value
- The default is a pale tint of the Windows highlight colour (about a quarter highlight on the window
colour), which follows the colour scheme. Set
Color.Emptyto return to it.
Remarks
Hot buttons are also outlined in BorderColor.
Example
spinner.UpDownButtonNormalBackColor = Color.WhiteSmoke;
spinner.UpDownButtonOnFocusBackColor = Color.AliceBlue;
spinner.UpDownArrowPressedBackColor = Color.LightSteelBlue;
spinner.ShowBorderAlways = false; // so the resting colour is seenMethods
CreateHandle
Creates the control's window.
Remarks
When the window is being created again – after DrawStyleIsFlat changes, for example – the
control takes over the drawing of the new text box and spin button windows at the next paint.
OnFontChanged
Raises the Control.FontChanged event, then fits the spin button to the new font.
Parameters
e- The event data.
See also
OnGotFocus
Raises the Control.GotFocus event and draws the border and buttons in their hot colours.
Parameters
e- The event data.
OnLayout
Raises the Control.Layout event, then fits the spin button to the control's height and font.
Parameters
e- The event data.
Remarks
The spin button is as wide as a vertical scroll bar or 1.1 times the font's height, whichever is wider,
but never more than a third of the control's width; the text box takes the rest. It moves to the left side when
Control.RightToLeft is Yes.
OnLostFocus
Raises the Control.LostFocus event and, unless ShowBorderAlways is set,
returns the border and buttons to their resting colours.
Parameters
e- The event data.
OnSystemColorsChanged
Raises the Control.SystemColorsChanged event when the Windows colour scheme changes.
Parameters
e- The event data.
Remarks
Button colours that have not been set are worked out from the system colours each time they are drawn, so they follow the new scheme.
WndProc
Processes the window's messages, drawing the flat border while DrawStyleIsFlat is set.
Parameters
m- The message to process.
Remarks
An override must call the base method for every message it does not handle completely, or the border and buttons are not drawn.