Attach UpDown Control to Textbox

Attach UpDown Control to Textbox

In Windows Common controls, we have an UpDown control. It can be attached toany textbox. It gives the user the appearance that he or she can control (increment or decrement) the textbox’s text by clicking the Up or Down arrows of the control.

However, the textbox can still have non-numeric text in it, which is notdesirable. This happens because the UpDown control can be bound to any otherproperty of the textbox also, but it hardly is used with any propertyother than the Text property of the textbox. So, if a control can restrict the user from entering data that is not valid, it can be of great help.

The following code does the same. It is to be used inside an Activex Controlproject. Use it for the following:

  • Properties like MinValue, MaxValue, Value and Increment—all double datatypes
  • Validating the text in the textbox to be within the specifiednumeric region (i.e., >= Min Value, <= Max Value, and above all it should bea valid number)
  • Raising a ValueChanged event whenever the value of the control ischanged

To create the control, open a new ActiveX Control project; add a textbox toIt; name it txtCounter; and paste the following code in the code window of theControl:

 Option ExplicitDim dblIncrement As DoubleDim dblMinValue As DoubleDim dblMaxValue As DoubleDim dblValue As DoublePublic Event ValueChanged(NewValue As Double)'Increment propertyPublic Property Let Increment(prmIncrement 
As Double) dblIncrement = prmIncrementEnd PropertyPublic Property Get Increment() As Double Increment = dblIncrementEnd Property'MinValue PropertyPublic Property Let MinValue(prmMinValue
As Double) If prmMinValue <= dblMaxValue Then dblMinValue = prmMinValue Call CorrectValue(dblValue) End IfEnd PropertyPublic Property Get MinValue() As Double MinValue = dblMinValueEnd Property'Max value PropertyPublic Property Let MaxValue(prmMaxValue
As Double) If prmMaxValue >= dblMinValue Then dblMaxValue = prmMaxValue Call CorrectValue(dblValue) End IfEnd PropertyPublic Property Get MaxValue() As Double MaxValue = dblMaxValueEnd Property'Value PropertyPublic Property Let value(prmValue As Double) Call CorrectValue(prmValue)End PropertyPublic Property Get value() As Double value = dblValueEnd PropertyPrivate Sub txtCounter_Change() If Not (IsNumeric(txtCounter.Text)) Then 'If the typed in is not numeric, restore it
to the last value Beep txtCounter.Text = CStr(dblValue) Else 'if typed in value is numeric, correct the
value and display CorrectValue (CDbl(txtCounter.Text)) End IfEnd SubPrivate Sub txtCounter_KeyDown(KeyCode
As Integer, Shift As Integer) Select Case KeyCode Case vbKeyUp Call CorrectValue(CDbl(txtCounter.Text)
+ dblIncrement) Case vbKeyDown Call CorrectValue(CDbl(txtCounter.Text)
- dblIncrement) End SelectEnd SubPrivate Sub txtCounter_LostFocus() CorrectValue (CDbl(txtCounter.Text))End SubPrivate Sub UserControl_ReadProperties(PropBag
As PropertyBag) dblMinValue = PropBag.ReadProperty("MinValue", 0) dblMaxValue = PropBag.ReadProperty("MaxValue", 0) dblIncrement = PropBag.ReadProperty("Increment", 0) txtCounter.Text = PropBag.ReadProperty("Value", 0)End SubPrivate Sub UserControl_Resize() txtCounter.Move 0, 0, UserControl.ScaleWidth,
UserControl.ScaleHeightEnd SubPrivate Sub UserControl_WriteProperties(PropBag
As PropertyBag) PropBag.WriteProperty "MinValue",
dblMinValue, 0 PropBag.WriteProperty "MaxValue",
dblMaxValue, 0 PropBag.WriteProperty "Increment",
dblIncrement, 0 PropBag.WriteProperty "Value", dblValue,
0End SubPrivate Sub CorrectValue(prmValue As Double) If prmValue > dblMaxValue Then 'Do not allow the value to increase beyond
the MaxValue Property Beep dblValue = dblMaxValue txtCounter.Text = CStr(dblValue) RaiseEvent ValueChanged(dblValue) 'Raise
the ValueChanged event ElseIf prmValue < dblMinValue Then 'Do not allow the value to decrease beyond
the MinValue Property Beep dblValue = dblMinValue txtCounter.Text = CStr(dblValue) RaiseEvent ValueChanged(dblValue)
'Raise the ValueChanged event Else If prmValue <> dblValue Then dblValue = prmValue txtCounter.Text = CStr(dblValue) RaiseEvent ValueChanged(dblValue)
'Raise the ValueChangedevent Else txtCounter.Text = CStr(dblValue) End If End IfEnd Sub
See also  11 Effective Keyword Research Tools and Techniques

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist