¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Spin buttons? Read This Bruce


Scott
 

Bruce,

If You Need More Help Just Hollar. I have an UP DOWN sample project if you need it...

The UpDown Control

The UpDown control offers a simple but effective way to create those spin buttons that many Windows applications display to the right of numeric fields and that let users increment or decrement a field's value using mouse clicks. While it's a trivial matter to create such spin buttons yourself?using a tiny VScrollBar control, for example, or using two smaller buttons with Style = 1-Graphical?the UpDown control offers many advantages and is far easier to set up and use than any other solution.

The most intriguing characteristic of the UpDown control is that you can link it to another control?its buddy control?at design time, and you can even select which particular property of the buddy control is affected by the UpDown control. Add to this the ability to set the scrolling range and the increment, and you see that in most cases you don't even need to write code to make everything work as expected.

Setting Design-Time Properties

In the General tab of an UpDown control, you typically set the Alignment property, which determines where the UpDown control has to align with respect to its buddy control. (The values are 0-cc2AlignmentLeft and 1-cc2AlignmentRight.) In this tab, you also set the Orientation property (0-cc2OrientationVertical or 1cc2OrientationHorizontal). The Orientation property can be set only at design time and is read-only at run time.

You select the buddy control in the Buddy tab of the Property Pages dialog box. (See Figure 11-2.) You can either type the control's name in the first field or tick the AutoBuddy check box. In the latter case, the UpDown control automatically selects the previous control in the TabIndex sequence as its buddy control. After you've selected a buddy control, two other fields on the Property Pages dialog box become available. In the BuddyProperty combo box, you select which property of the buddy control is affected by the UpDown control. (If you don't select any, the buddy control's default property is used.) You can set the SyncBuddy property to True, which causes the UpDown control to automatically modify the selected property in its buddy control.

You usually select a TextBox control as the buddy control of an UpDown control and Text as the buddy property. But nothing prevents you from connecting an UpDown control to other properties (for example, Left or Width) exposed by other types of controls. You can't use lightweight windowless controls as buddy controls, however.

Figure 11-2. The Buddy tab of the Property Pages dialog box of an UpDown control lets you select the buddy control and the buddy property.

Finally, in the Scrolling tab of the Property Pages dialog box, you select the UpDown control's Min and Max properties, which identify the valid range for the Value property. The Increment property is the value that's added to or subtracted from the Value property when the user clicks on the UpDown control's spin buttons. If the Wrap property is set to True, the Value property wraps when it reaches the Min or Max value.

Run-Time Operations

If the UpDown control's SyncBuddy property is set to True, you don't need to write any code to manually change the property in the buddy control. There are cases, however, when you can't rely on this simple mechanism. For example, the UpDown control might have no buddy controls or perhaps it's supposed to affect multiple controls or multiple properties of the same control. (For example, you might need to enlarge or shrink another control by affecting its Width and Height properties at the same time.) In such cases, all you have to do is write code inside the Change event procedure, as you would do for a scroll bar control.

The UpDown control exposes two custom events that give you even more flexibility: The DownClick and UpClick events, which fire when the mouse is released (that is, after the Change event) on either one of the buttons that make up the UpDown control. These events fire even if the Value property has already reached its Min or Max, which makes DownClick and UpClick events useful when you don't want to enforce a limit to the range of valid values:

?

' Move all controls on the form pixel by pixel.
Private Sub UpDown1_DownClick()
    Dim ctrl As Control
    For Each ctrl In Controls
        ctrl.Top = ctrl.Top + ScaleY(1, vbPixels, vbTwips)
    Next
End Sub
Private Sub UpDown1_UpClick()
    Dim ctrl As Control
    For Each ctrl In Controls
        ctrl.Top = ctrl.Top - ScaleY(1, vbPixels, vbTwips)
    Next
End Sub

All the properties that you set at design time can also be modified at run time through code, with the exception of the Orientation property. For example, you can change the buddy control and the buddy property using this code:

?

Set UpDown1.BuddyControl = Text2
UpDown1.BuddyProperty = "Text"

The BuddyControl property can also be assigned the name of the buddy control, for example:

?

UpDown1.BuddyControl = "Text2"
' This syntax even works with items of control arrays.
UpDown1.BuddyControl = "Text3(0)"

When you change the buddy control at run time, the UpDown control automatically moves to a position beside its buddy control, which shrinks to make room for the UpDown control.



Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes