What is a Combo Box?
One often used graphical user interface widget (or control) is a combo box. Sometimes, a "drop-down list" is referred to as a "combo box". "Drop-down list" and "combo box" are not interchangeable terms in Java or. NET. In order to differentiate the definition of "drop-down list" from "combo box," phrases like "non-editable combo box" (or anything similar) are occasionally used.
![What is a Combo Box?](https://static.tutorialandexample.com/computer-fundamentals/what-is-a-combo-box1.png)
Depending on the circumstance, combination boxes may be indicated as necessary or optional. There are two stylistic options available for needed combo boxes: an asterisk or a label that reads "(required)". Make sure you provide clue text explaining the meaning of the asterisk if you use one. Optional combo boxes are either indicated with nothing at all or with text appended to the end of the label, such as "(optional)".
The asterisk in this component is not a literal element of the label text; rather, it is an icon placed at a certain distance from the label text. Additionally, combo boxes can automatically finish the input field's suggested results.
Types of Combo Box
Combo boxes come in three different varieties. Working with combo boxes generally follows the same guidelines for all of the types as mentioned earlier:
- A list box attached to an edit box makes up a basic combo box. The item list is constantly visible. When selecting an item, you have two options: input the item's caption or use keyboard keys to move about the combo box control. With a straightforward combo box, you can type any text. This item will be chosen if the entered caption matches any of the entries in the combo box.
- A drop-down list box is made up of a list of items represented by a drop-down list and a static text box that shows the item that is now selected. When utilizing a drop-down list box, your options are limited to the specified list. Incremental search is supported via a drop-down list box. This implies that while you type, the control will look for the item and choose the relevant one if the text you input matches any of the items.
- A drop-down combo box combines an edit box and a drop-down list so that you can choose an item from the drop-down list or input text for an item. Any text can be typed. This item will be chosen if it matches any of the items in the combo box. Owner-drawn combo boxes differ in a few ways from normal ones.
Combo Box List
The part of a combo box that shows the options available to the user is called the list. When an application generates a combo box, it usually initializes the items on the list. In this situation, the item is not the current selection; if it is suitable to do so, the application should add it to the list and make it the current selection.
These subjects are covered in this section:
- Current Selection
- Drop-down Lists
- List Contents
1. Current Selection
The user-chosen list item is the current selection; the selected text is displayed in the combo box's selection field. The current selection is just one way that a user can enter data into a combo box, though, whether it's a drop-down or simple combo box. Text entry is also permitted in the selection area.
![What is a Combo Box?](https://static.tutorialandexample.com/computer-fundamentals/what-is-a-combo-box2.png)
The selected list item's zero-based index identifies the current selection. When a user modifies a combo box's current selection, a notification is sent to the parent window or dialogue box operation. The application does not notify the parent window or dialogue box when the selection is altered.
A combination box has no selection when it is first formed. This also applies to a straightforward or drop-down combo box in the event that the user modifies the selected field. To set the current selection, an application sends the CB SETCURSEL message to the combo box. Applications can also use the CB SELECTING message to set the current selection to a list item whose string begins with a specified string. To obtain the current selection, an application communicates with the combo box via the CB GETCURSEL message. This message returns CB_ERR if there isn't a selection active at the moment.
When a drop-down list closes, a drop-down combo box or drop-down list box sends the CBN_CLOSEUP notification code to the parent window or dialog-box function. When the drop-down list closes, the combo box additionally transmits the CBN_SELCHANGE notification code if the user modified their current selection. You can handle either the CBN_SELCHANGE or CBN_CLOSEUP notification code to have a specific action run every time the user selects an item in the list. Normally, you would only execute a change in the current selection once you received the CBN_CLOSEUP notification code. This may be especially crucial if a substantial amount of processing is needed.
The notification codes CBN_SELENDOK and CBN_SELENDCANCEL may also be processed by an application. When a user chooses an item from the list or chooses an item and then closes the list, the system sends CBN_SELENDOK. When a user picks an item and subsequently chooses a different control, hits ESC while the drop-down list is visible, or exits the dialogue box, CBN_SELENDCANCEL is generated. This suggests that the choice made by the user should be disregarded. Before each CBN_SELCHANGE message, CBN_SELENDOK is sent.
The CBN_DBLCLK notification code is transmitted by the system when a user double-clicks a list item in a basic combo box. It is not possible to double-click an item in a drop-down list or combo box because the list is hidden with a single click.
2. Drop-down Lists
Only combo boxes with drop-down lists are subject to specific alerts and messages. The parent window of a combo box is notified when a drop-down list is opened or closed by a WM_COMMAND message. It is CBN_CLOSEUP if the list is being closed. Applications can use the CB_SHOWDROPDOWN message to open a list of drop-down list boxes or combo boxes. It can use the CB_GETDROPPEDSTATE message to find out if the list is open and the CB_GETDROPPEDCONTROLRECT message to find out the coordinates of a drop-down list. Moreover, an application can use the CB_SETDROPPEDWIDTH message to expand a drop-down list's width.
3. List Contents
When a combo box is created by an application, it usually adds one or more items to the list to initialize the combo box. Subsequently, an application can retrieve item information from the list, add or remove things from it, or reinitialize the list.
A combo box can have a list of items added to it by an application sending the CB_ADDSTRING message to it. When an item is specified, it is either appended to the end of the list or, in a sorted combo box, it is sorted according to its string and placed in the appropriate spot. An application can use the CB_INSERTSTRING message to insert an item at a precise place in an unsorted combo box. A list item is recognized by its location once it has been inserted.
The position of a list item can be found by an application using the CB_FINDSTRING or CB_FINDSTRINGEXACT message. The function CB_FINDSTRING locates an item whose string starts with the given string. The function CB_FINDSTRINGEXACT locates an item whose string precisely matches the string. The messages are not case-sensitive.
The CB_DELETESTRING message can be used by an application to eliminate a record from a list. The combo box list can be completely cleared by an application using the CB_RESETCONTENT message before being reinitialized. In that case, an application can remove the redraw flag to prevent the combo box from being painted again as each item is added. See the WM_SETREDRAW message description for further details on redrawing.
Using the CB_GETLBTEXT message, an application can obtain the string linked to a list item. The string of the item is transferred to the buffer that the application designates. The program can first use the CB_GETLBTEXTLEN message to find out how long the string is, ensuring that the buffer is big enough to hold the string. Using the CB_GETCOUNT message, an application can determine how many list items are present in a combo box.