Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Intermediate
Aug 25, 2000

Attach UpDown Control to Textbox

In Windows Common controls, we have an UpDown control. It can be attached to any 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 not desirable. This happens because the UpDown control can be bound to any other property of the textbox also, but it hardly is used with any property other 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 Control project. 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 specified numeric region (i.e., >= Min Value, <= Max Value, and above all it should be a valid number)
  • Raising a ValueChanged event whenever the value of the control is changed
To create the control, open a new ActiveX Control project; add a textbox to It; name it txtCounter; and paste the following code in the code window of the Control:
 Option Explicit
Dim dblIncrement As Double
Dim dblMinValue As Double
Dim dblMaxValue As Double
Dim dblValue As Double

Public Event ValueChanged(NewValue As Double)

'Increment property
Public Property Let Increment(prmIncrement 
As Double) dblIncrement = prmIncrement End Property Public Property Get Increment() As Double Increment = dblIncrement End Property 'MinValue Property Public Property Let MinValue(prmMinValue
As Double) If prmMinValue <= dblMaxValue Then dblMinValue = prmMinValue Call CorrectValue(dblValue) End If End Property Public Property Get MinValue() As Double MinValue = dblMinValue End Property 'Max value Property Public Property Let MaxValue(prmMaxValue
As Double) If prmMaxValue >= dblMinValue Then dblMaxValue = prmMaxValue Call CorrectValue(dblValue) End If End Property Public Property Get MaxValue() As Double MaxValue = dblMaxValue End Property 'Value Property Public Property Let value(prmValue As Double) Call CorrectValue(prmValue) End Property Public Property Get value() As Double value = dblValue End Property Private 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 If End Sub Private 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 Select End Sub Private Sub txtCounter_LostFocus() CorrectValue (CDbl(txtCounter.Text)) End Sub Private 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 Sub Private Sub UserControl_Resize() txtCounter.Move 0, 0, UserControl.ScaleWidth,
UserControl.ScaleHeight End Sub Private 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,
0 End Sub Private 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 ValueChanged event Else txtCounter.Text = CStr(dblValue) End If End If End Sub
Jatin Shah
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date