Wilson Kutegeka

Microsoft MVP - Visual Basic www.clinicmaster.net

Custom Controls - NumericBox

Introduction


NumericBox Control: This is one of the many custom controls in VB from my tool box that I intend to share with you. It’s actually a text box control that receives only numeric keys from the keyboard and only numeric data from the clipboard.


Well there are many numeric controls that you can find out there, but this particular one has some special features such as controlling the numeric data type you enter, denying the pasting of data that can’t convert to a specified data type and of course it’s in VB. You can as well set visual styles, data formats, minimum and maximum values etc.


Members of interest of NumericBox include the following:-

Properties

  • ControlCaption

This property sets or gets the text message that will be part of error message if control has invalid entry. E.g. if this value is set as Age and MaxValue property of the control is set to 100 and MinValue property set to 0, if you enter a value such as 200, you get a message that reads “Age must be between 0 and 100” a typical value for ControlCaption include the label text besides this control.

  • DataFormat

This property sets or gets the data format of controls’ value possible values include General, Standard, Currency and Percent. DataFormat is only applicable if DataType is set to Single, Double or Decimal.

  • DataType

This property sets or gets the data type of controls’ value. Possible values include Short (Int16), Integer (Int32), Long (Int64), Single, Double and Decimal. The exact data type is returned by method Get[DataType].

  • DecimalPlaces

This property sets or gets the number of decimal places for the entered value. This property is only applicable to data types such as Single, Double, Decimal and to specific Data formats.

  • MustEnterNumeric

Some times the user may want to work with none numeric values such as percentages and/or null values. Thus MustEnterNumeric property allows you to work with such values.

  • Value

This property Mimics the Text property of a textbox. However, it will not allow you to enter non-numeric. 

Note 
1.  Text property is not visible in the properties window.
2. Value Property returns a string value. It’s made so in order to support null values and others such as percentages otherwise, you must use Get[DataType] method to receive the exact data type

  • VisualStyle

This property sets or gets the Visual style of the control possible values include None, Standard, OfficeXP, Office2003. VisualStyle is not yet at the level I’d want it to be, I will be releasing an enhanced version later.

  • MinValue

This property sets or gets the Minimum value allowed in control. Applicable only if MaxValue is set greater than MinValue

  • MaxValue

This property sets or gets the Maximum value allowed in control. Applicable only if MaxValue is set greater than MinValue

Methods   

  • GetValue()

This method returns the controls’ value, and as an object data type, however this value will successfully convert to the data type specified in the DataType property and, if MustEnterNumeric is set to false, this method will return that value as string.

The following methods will return the numeric control’s value as datatype as is in the method’s name

  • GetShort()
  • GetInteger()
  • GetLong()
  • GetSingle()
  • GetDouble()
  • GetDecimal()

Note
Remember to always wrap these methods in the try catch block. They throw exceptions if the entered data can’t be converted to a specified data type.

Other Feature(s) Include

  • Denial of pasting of value that can’t convert to the specified data type
  • Allowing data only from numeric keys
    etc 

You can download the source code below
(Usage example in both VB and C# is included)

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 2 and 3 and type the answer here: