Up-Down Controls

An up-down control is a small window containing up and down arrows that the user can click to increment or decrement a value. An up-down control is similar to a scroll bar, but it consists only of arrows. (It also has a sillier name.) You can use an up-down control alone as a simplified scroll bar or with another control (called a buddy control—yet another silly name). In Figure 2-6 on the next page, the up-down control is paired with an edit control to create a spin box; when the user clicks an arrow or presses an arrow key, the up-down control increments or decrements the value in the edit control. You can use any type of control as the designated buddy control, however.

Figure 2-6.

A dialog box that uses an up-down control and a buddy control.

The range of an up-down control specifies the upper and lower bounds for the position (the integer the user adjusts with the up and down arrows). Unlike a scroll bar's position, the position of an up-down control is updated automatically. When the positional value is updated, the buddy control is also automatically updated if the up-down control has the UDS_AUTOBUDDY style. If the upper bound is less than the lower bound, clicking the up arrow decrements the position, and clicking the down arrow increments it.

You can specify various window styles to determine the characteristics of an up-down control or its buddy control. For example, you can change the way the up-down control positions itself relative to its buddy control (the UDS_ALIGNLEFT style), determine whether it sets the text of its buddy control (the UDS_SETBUDDYINT style), or determine whether it processes the Up and Down arrow keys on the keyboard (the UDS_ARROWKEYS style).

By default, the position of an up-down control does not change if the user attempts to increment or decrement it beyond the upper or lower bound. You can change this behavior by using the UDS_WRAP style, which wraps the position to the opposite extreme. (For example, if your range is 1 through 10, incrementing the position past 10 wraps it back to 1.)

The range of an up-down control cannot exceed 32,767 positions. You can invert the range—that is, the lower bound of the range can be greater than the upper bound. Note, however, that the up arrow always moves the current position toward the upper bound, and the down arrow always moves the current position toward the lower bound. If the range is 0 (the lower bound is equal to the upper bound) or the control is disabled, the control draws dimmed arrows.

The buddy control must have the same parent as the up-down control. If you use the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style and the buddy control resizes, you must send the UDM_SETBUDDY message (or call the MFC SetBuddy member function) to re-anchor the up-down control on the appropriate border of the buddy control. The UDS_AUTOBUDDY style calls the GetWindow function with GW_HWNDPREV to choose the buddy control. In the case of a dialog resource, the UDS_AUTOBUDDY style chooses the previous control listed in the resource script. If the z-order of the windows changes, sending a UDM_SETBUDDY message with a NULL handle causes a new buddy to be selected; otherwise, the original autobuddy choice is maintained.

The UPDOWN sample, which I wrote to demonstrate up-down controls, allows the user to change the number of times the word Welcome! is written to the client area of the screen. Figure 2-7 shows the UPDOWN sample.

Figure 2-7.

The UPDOWN sample.