Visual Basic Concepts

Using the UpDown Control

See Also

An UpDown control is a pair of arrow buttons that the user can click to increment or decrement a value, such as a scroll position or a number displayed in a buddy control. The buddy control can be any other kind of control, as long as it has a property that can be updated by the UpDown control.

To the user, an UpDown control and its buddy control often look like a single control. You can specify that an UpDown control automatically position itself next to its buddy control and that it automatically set the value of the buddy control to its current value. For example, you can use an UpDown control with a Textbox control to prompt the user for numeric input. The illustration below shows an UpDown control with a Textbox control as its buddy control, a combination that is sometimes referred to as a spinner control.

Possible Uses

Basic Operation

To use the UpDown control, you must first set the BuddyControl property to another control, and the BuddyProperty to a scrollable property on the other control. The Min and Max properties determine the limits of the control, and the Wrap property determines if the value will wrap when the end user scrolls past the Min or Max values. The Increment property specifies the amount by which the Value property will be changed when either the up or down arrow is clicked.

Use the AutoBuddy Property to Set a Buddy Control Automatically

At design time, setting the AutoBuddy property to True causes the UpDown control to automatically "buddy" with the previous control in the TabOrder.

To automatically set a buddy control

  1. Draw the buddy control on the form.

  2. Draw the UpDown control on the form.

  3. Right-click the UpDown control and click Properties to display the Property Pages dialog box.

  4. Click Buddy to display the Buddy tab, as seen in Figure 2.43 below.

  5. Click the AutoBuddy check box to set the AutoBuddy property to True.

  6. Click the SyncBuddy check box to set the SyncBuddy property to True.

Figure 2.43   Buddy Tab on the UpDown control property pages

Determining the Scroll Behavior

The UpDown control includes several events and properties that allow you to determine how the control scrolls.

The Increment and Wrap Properties

The Wrap property determines how the control will behave when the end user holds down either the up or down button. If set to False, when the Max or Min value is reached, the controls ceases to scroll, and stops at the Max or Min value. When set to True, the control will return to either the Min or Max value and continue to increment (or decrement) through the values as determined by the Min and Max properties.

The Increment property specifies the amount by which the Value property will be changed when either the up or down arrow is clicked. This value cannot be a negative number. One use of this is to toggle a field or OptionButton between True (-1) and False (0), as shown in the code below:

Private Sub Form_Load()
   ' The UpDown control is named "updToggle."
   ' An OptionButton control is named "optToggle."
   With updToggle
      .BuddyControl = optToggle
      .Min = -1
      .Max = 0
      .Increment = 1
      .Wrap = True
   End With
End Sub

' Change the Value of the OptionButton with
' the UpDown control's Value property.
Private Sub updToggle_Change()
   optToggle.Value = updToggle.Value
End Sub

The UpClick and DownClick Events

Using the UpClick and DownClick events, you can control exactly how the UpDown control scrolls through a series of values. For example, if you want to allow the end user to scroll rapidly upward through the values, but slower going down through the values, you can set reset the Increment property to different values, as shown below:

Private Sub updScroll_UpClick()
   ' The name of the UpDown controls is "updScroll"
   updScroll.Increment = 5
End Sub

Private Sub updScroll_DownClick()
   ' When the user clicks the down button, the
   ' Increment switches to 1 for finer resolution.
   updScroll.Increment = 1
End Sub