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
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.