Creating Combo Boxes
Windows Predefined Controls – Part 25
Volume - Windows User Interface
Forward: In this part of the series, we look at the creation of combo boxes.
By: Chrysanthus Date Published: 30 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
A Combo Box
A combo box is a control that consist of a list box and either an edit control or a static control above it. With the combo box only one item can be selected; that is, the user cannot select more than one item in the list box at the same time. When the item is selected it appears in the edit control or the static control, depending on which one the combo box is using. All controls are child windows.
The edit control or the static control above the list box is called the Selection Field. The list box of the combo box is simply called the list. The list presents the options that a user can select, and the selection field displays the option selected. The option selected is called the current selection.
If the selection field is an edit control, the user can type what he wants directly into the edit control. What he types into the edit control does not necessarily have to be found in the list.
In this series, we deal only with items in the list that are strings.
There are three types of combo boxes: Simple, Drop Down and Drop Down List. We saw the descriptions of these types in one of the previous parts of the series. For the simple combo box, you have the CBS_SIMPLE style. For the drop down combo box, you have the CBS_DROPDOWN style. For the drop down list combo box, you have the CBS_DROPDOWNLIST style. The combo box, whatever the type, is of the operating system class, COMBOBOX.
You can create a combo box using the CreateWindowEx function. After that you have to send each item string to the box using the SendMessage function. In this series, we are dealing with strings as items in the combo box list. The message to add a string to a combo box list is CB_ADDSTRING.
This message adds a string to a list box. If the list box does not have the CBS_SORT style, the string is added to the end of the list. Otherwise, the string is inserted into the list and the list is sorted. The wParam is not used (set it to NULL). The lParam is of type LPCTSTR and is a pointer to the null-terminated string to be added. The return value is the zero-based index to the string in the list box of the combo box. If an error occurs, the return value is CB_ERR. If insufficient space is available to store the new string, it is CB_ERRSPACE.
Here is an example program to create a simple list box:
#include <windows.h>
using namespace std;
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = MainWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hinstance;
wcx.hIcon = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "MainWClass";
wcx.hIconSm = NULL;
RegisterClassEx(&wcx);
HWND hwndMain;
hwndMain = CreateWindowEx(0, "MainWClass", "Main Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance, NULL);
if (!hwndMain)
return FALSE;
ShowWindow(hwndMain, SW_SHOW);
UpdateWindow(hwndMain);
const char *str1 = "the first string.";
const char *str2 = "the second string.";
const char *str3 = "the third string.";
const char *str4 = "the fourth string.";
const char *str5 = "the fifth string.";
HWND hwndCb;
hwndCb = CreateWindowEx(0, "COMBOBOX", NULL, WS_CHILD|CBS_SIMPLE, 100, 100, 100, 150, hwndMain, (HMENU)1, hinstance, NULL);
ShowWindow(hwndCb, SW_SHOW);
UpdateWindow(hwndCb);
SendMessage(hwndCb, CB_ADDSTRING, NULL, (LPARAM)str1);
SendMessage(hwndCb, CB_ADDSTRING, NULL, (LPARAM)str2);
SendMessage(hwndCb, CB_ADDSTRING, NULL, (LPARAM)str3);
SendMessage(hwndCb, CB_ADDSTRING, NULL, (LPARAM)str4);
SendMessage(hwndCb, CB_ADDSTRING, NULL, (LPARAM)str5);
MSG msg;
BOOL bRet;
while( (bRet = GetMessage( &msg, hwndMain, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit the application
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
g++ wincb.cpp -mwindows -o wincb.exe
You can add scrollbars using the windows styles, WS_VSCROLL and/or WS_HSCROLL (see later).
Creating a Drop Down Combo Box
To create a drop down combo box, replace the CBS_SIMPLE style in the above program with CBS_DROPDOWN.
Creating a Drop Down List Combo Box
To create a drop down list combo box, replace the CBS_SIMPLE style in the above program with CBS_DROPDOWNLIST.
Of course you can add other combo box styles to any of the above styles.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT