Microsoft KB Archive/74792

{|
 * width="100%"|

HOWTO: Make a List Box Item Unavailable for Selection

 * }

Q74792

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK)
 * Microsoft Windows Software Development Kit (SDK) 3.1

-

SUMMARY
In the Microsoft Windows graphical environment, an application can use a list box to enumerate options. However, there are circumstances in which one or more options may not be appropriate. The application can change the appearance of items in a list box and prevent the user from selecting one of these items by using the techniques discussed below.

Changing the Appearance of a List Box Item
To dim (gray) a particular item in a list box, use an owner-draw list box as follows:

 Create a list box that has the LBS_OWNERDRAW and LBS_HASSTRINGS styles.  Use the following code to process the WM_MEASUREITEM message:

     case WM_MEASUREITEM: ((MEASUREITEMSTRUCT FAR *)(lParam))->itemHeight = wItemHeight; break; wItemHeight is the height of a character in the list box font.  Use the following code to process the WM_DRAWITEM message:

Strings that start with &quot;!&quot; are displayed dimmed. The exclamation mark character is not displayed.

Preventing Selection
To prevent a dimmed string from being selected, create the list box with the LBS_NOTIFY style. Then use the following code in the list box's parent window procedure to process the LBN_SELCHANGE notification:

  case WM_COMMAND:

switch (wParam) {

...

case IDD_LISTBOX: if (LBN_SELCHANGE == HIWORD(lParam)) {         idx = (int)SendDlgItemMessage(hDlg, wParam,                        LB_GETCURSEL, 0, 0L); SendDlgItemMessage(hDlg, wParam, LB_GETTEXT, idx,                       (LONG)(LPSTR)szBuf); if ('!' == *szBuf) {           // Calculate an alternate index here // (not shown in this example).

// Then set the index. SendDlgItemMessage(hDlg, wParam, LB_SETCURSEL, idx, 0L); }         }        break;

...

} break; When the user attempts to select a dimmed item, the alternate index calculation moves the selection to an available item.

Additional query words: listbox WIN16SDK

Keywords : kbCtrl kbListBox kbOSWinNT kbOSWin2000 kbSDKWin32 kbGrpDSUser kbOSWin

Issue type : kbhowto

Technology : kbWin32SDKSearch kbAudDeveloper kbWin3xSearch kbSDKSearch kbWin32sSearch kbWinSDKSearch kbWinSDK310