Spinner Controls

The spinner is a modified list box control buddied with an up-down control. The result is a single line control that can be spun to display each item in the list box. Unlike the standard list box, the spinner control uses the Left and Right cursor keys to spin between items in the box. As with other Smartphone controls, the Up and Down cursor keys transfer focus to the next control in the tab order.

Creating a spinner consists of creating both the list box and up-down controls. In a dialog resource, the combination would look like the following:

                  UDS_SETBUDDYINT | UDS_WRAP | UDS_EXPANDABLE, 0, 0, 0, 0

The first line defines the list box. The only unusual thing about the declaration is that the list box is only 12 dialog units high, just tall enough for one item. The up-down control is declared immediately after the list box. The CONTROL resource tag is used for this control because the resource compiler doesn't use a specific keyword such as LISTBOX to declare an up-down control. The large number of style flags configures the control for this specific use. Many of the same style flags are used when creating expandable edit controls, so I'll only cover the new ones. The UDS_HORZ and flag tells the control to create Left and Right arrows instead of Up and Down arrows attached to the list box.

The UDS_SETBUDDYINT flag tells the up-down control to manipulate the text of its buddy, in this case the list box, and have it scroll among the different items in the list. The UDS_WRAP flag tells the control to wrap the list so that if the list box is at the last item in the list and the user presses the right button, the list box will show the first item in the list.

The up-down control can have the additional style flag of UDS_NOSCROLL. This flag prevents the user from spinning the data with the Left and Right cursor keys. This style isn't much use unless it's combined with the UDS_EXPANDABLE flag so the control can be expanded, allowing the user to change the selection. When the user expands the spinner, it sends a WM_NOTIFY message to the parent window with the UDN_EXPANDING command. Figure 19-8 shows a spinner control in both normal and expanded modes.

Figure 19-8

A spinner control in normal and expanded modes

A spinner can also be created manually with two calls to CreateWindow, as shown here:

HWND hwndList = CreateWindow (TEXT("listbox"), NULL, WS_VISIBLE | 
                              WS_BORDER | WS_TABSTOP, 5, 5, 75, 20, hWnd,
                              (HMENU)IDD_LISTCITIES, hInst, 0L);

                                UDS_ALIGNRIGHT | UDS_ARROWKEYS |
                                UDS_SETBUDDYINT | UDS_WRAP | UDS_EXPANDABLE,
                                0, 0, 0, 0, hWnd, 0, hInst, 0L);

SendMessage (hwndUpDown, UDM_SETBUDDY, (WPARAM)hwndList, 0);

Here, like in the expandable edit control, the only difference between the two methods is the extra message sent to the up-down control to tell it the ID of its buddy list box and a few manually added style flags needed when creating the list box.

This topic is from Programming Microsoft Windows CE, Third Edition, by Douglas Boling, published by Microsoft Press. © 2003 by Douglas McConnaughey Boling. Reprinted here by permission of the author.