When you add or create a combo box, an amount of space is allocated and during the lifetime of your application, the combo box uses some memory to perform its assignments. If at one time there is not enough memory for a processing, the combo box sends an ON_CBN_ERRSPACE message. The event related to this message doesn't take an argument. Like any control, for the user to use the combo box, it must first have focus. This can be done by the user clicking its edit part, its list part (for a Simple combo box) or its down pointing arrow (for a drop-type combo box). When the combo box receives focus, it sends the ON_CBN_SETFOCUS message. The event related to this message doesn't take an argument. Once the control has focus, if the user clicks or had clicked the edit control side of the combo box that already had a selected item, if the user starts typing, which would modify the string of the item that was already selected, the combo box would send the ON_CBN_EDITCHANGE message (remember that the user can change the string only if the combo box was not created as Drop List). The event related to this message doesn't take an argument. If the user finishes typing or changing the string, just before the altered string is validated, the combo box sends the ON_CBN_EDITUPDATE message. The event related to this message doesn't take an argument.
After adding a combo box, if you want to immediately create its strings, in the Properties window, click the Data field. To create the list of items, type the items separated by a semi-colon. An example would be: Small;Medium;Large;Jumbo To let you programmatically add a string to a combo box, the CComboBox class is equipped with a member function named AddString. Its syntax is: int AddString(LPCTSTR lpszString); This member function takes one argument as the string to be added to the combo box. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;
Majors->Create(WS_CHILD | WS_VISIBLE,
CRect(10, 50, 100, 150),
this, 0x1448);
Majors->AddString(L"Accounting");
Majors->AddString(L"Art Education");
Majors->AddString(L"Finance");
Majors->AddString(L"Biology");
return TRUE; // return TRUE unless you set the focus to a control
}
This would produce:
To let you know the number of items in a combo box, the CComboBox class is equipped with the GetCount() member function. Its syntax is: int GetCount() const;
You can insert a string inside the list of items of a combo box. You will have to provide the 0-based position of the new string and its value. To support this, the CComboBox class is equipped with the InsertString() member function. Its syntax is: int InsertString(int nIndex, LPCTSTR lpszString); The first argument represents the index of the second argument. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;
Majors->Create(WS_CHILD | WS_VISIBLE,
CRect(10, 10, 160, 120),
this, 0x1448);
Majors->AddString(L"Accounting");
Majors->AddString(L"Art Education");
Majors->AddString(L"Finance");
Majors->AddString(L"Biology");
Majors->InsertString(1, L"Journalism");
return TRUE; // return TRUE unless you set the focus to a control
}
This would produce:
By default, the strings of a combo box are added to the list in a sequential order. If you want, you can make the control re-order its list. To do this visually, after adding the control to a dialog box, in the Properties window, set the value of the Sort field. If you are programmatically creating the control and you want to sort its items, add the CBS_SORT style. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;
Majors->Create(WS_CHILD | WS_VISIBLE | CBS_SORT,
CRect(10, 10, 160, 120),
this, 0x1448);
Majors->AddString(L"Biology");
Majors->AddString(L"Accounting");
Majors->AddString(L"Art Education");
Majors->AddString(L"Finance");
return TRUE; // return TRUE unless you set the focus to a control
}
This would produce:
If add some strings to a combo box, then programmatically sort the list, the items would be sorted. If you add a new item, it would be added to the end of the list. If you insert an item, it would be put in the position you indicated, ignoring the sorting. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;
Majors->Create(WS_CHILD | WS_VISIBLE | CBS_SORT,
CRect(10, 10, 160, 120),
this, 0x1448);
Majors->AddString(L"Biology");
Majors->AddString(L"Accounting");
Majors->AddString(L"Art Education");
Majors->AddString(L"Finance");
Majors->InsertString(1, L"Journalism");
return TRUE; // return TRUE unless you set the focus to a control
}
This would produce:
To select an item from the list, the user can click it. To let you programmatically select an item, the CComboBox class is equipped with a member function named SetCurSel. Its syntax is: int SetCurSel(int nSelect); This function takes as argument the 0-based index of the item you want to select. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CComboBox *Majors = new CComboBox;
Majors->Create(WS_CHILD | WS_VISIBLE | CBS_SORT,
CRect(10, 10, 165, 120),
this, 0x1448);
Majors->AddString(L"Biology");
Majors->AddString(L"Accounting");
Majors->AddString(L"Art Education");
Majors->AddString(L"Finance");
Majors->InsertString(1, L"Journalism");
Majors->SetCurSel(2);
return TRUE; // return TRUE unless you set the focus to a control
}
This would produce:
As mentioned already, to select an item from a drop type combo box, the user usually clicks the down pointing arrow button to display the control's list. When the user clicks that button, just before the list displays, the control sends the ON_CBN_DROPDOWN message. The event related to this message takes no argument. After this click and this message, the list part of a drop combo box displays and the user must make a decision:
Once the user has clicked an item or pressed Enter to validate a selection, if the combo box was created not as Simple, the list part of the control retracts to hide itself. At this time, the combo box sends an ON_CBN_CLOSEUP message. The event related to this message doesn't take an argument. If the user finishes using the combo box and moves to another control, the combo box sends an ON_CBN_KILLFOCUS message, notifying the application that it (the combo box) has lost focus. The event related to this message doesn't take an argument.
Instead of simply selecting an item from a combo box, the user may want to find out if a certain string exists in the list. To support this operation, the CComboBox class is equipped with a member function named FindString. Its syntax is: int FindString(int nStartAfter, LPCTSTR lpszString) const; This method takes as arguments the index where to start looking for a string. If the item is found in the list, the member function returns its position. If the list does not have that string, the member function return -1. The FindString() member function performs its operation without regards to case. This means that it would perform the same search for BlindMan, Blindman, blindMan, or BLINDMAN and would produce the same result for them. If you want the case of the characters to be taken into consideration, use the FindStringExact() member function. Its syntax is: int FindStringExact(int nIndexStart, LPCTSTR lpszFind) const; This member function proceeds like FindString() method by starting to look for the string from the beginning of the list.
To let you remove a string from a combo box, the CComboBox class is equipped with a member function named DeleteString. Its syntax is: int DeleteString(UINT nIndex); To let you delete all items from a combo box, the CComboBox class provides the Clear() member function. Its syntax is: void Clear(); |
|
|||||||||||||||||||||||
|
|