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
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as