 |
List Box |
|
 |
A list box presents a list of items to the user to
choose from with each item on its own line. The user makes a selection
by clicking in the list. Once clicked, an item becomes highlighted
indicating that it is the current choice. A list box can be configured
to allow single or multiple selections.
One of the main reasons for using a list box is to
display a list of items on specific lines to the user. Sometimes the
list would be very large. If the list is longer than the available space
on a form, the control would provide a scroll bar that allows the user
to navigate up and down and then access all items of the list. The
application developer decides how many items to display on the list.
|
|
To create a list box, you can use the List Box
button from the Controls
toolbox.
|
|
|
Testing the Program as you Progress
- To test the result, anytime, press Ctrl + F5.
Creating the Application
- Start Microsoft Visual C++
- On the main menu, click File -> New.
- Decide to create a Dialog Based application called
ListOfCountries with a title as
List of Countries
- While designing the dialog box, place a List Box control on it. Change the
identifier of the list box to IDC_LIST_COUNTRIES
- Place an Edit Box on the dialog. Call it IDC_LISTDBLCLK
- Right-click the List Box and click ClassWizard...
- Click the Member Variables property sheet.
- Double-click IDC_LISTCOUNTRIES to Add a Variable
- Set the Member Variable Name to m_ListCountries
- In the Category combo box, select Control

- Click OK
- Also Add a Variable for the IDC_LISTDBLCLK control and named
m_ListResult based on CString
Adding Items to a List Box
Select an Item Using its Position
Transfer an Item to an Edit Box After Double-Clicking in
the List
- Press Ctrl + W to access the ClassWizard.
- Click the Message Maps property sheet. In the Class Name combo box, make
sure CListExoDlg is selected. In the Object IDs, click IDC_LISTCOUNTRIES
- In the Messages list, double-click LBN_DBLCLK. Change the name of the
function to OnDblclkList and click OK.
- Click Edit Code and implement the function as follows:
void CExoListDlg::OnDblclkList()
{
m_ListCountries.GetText(m_ListCountries.GetCurSel(), m_ListResult);
UpdateData(FALSE)
}
Physically Transfer the Content of an Edit Box to a List
Box
We have learned how to transfer a double-clicked item from the List Box to an
Edit Box. The following is used if for some reason you don't want to (or can't)
use the DblClick function.
- On the dialog, add a button identified as IDC_BTN_TRANSFER
- Set its Caption to >>
- On its right side, add an Edit Box identified as IDC_EDIT_TRANSFER
- Press Ctrl + W to access the ClassWizard.
- In the Member Variables property sheet, double-click IDC_EDIT_TRANSFER
- Set the name of the variable as m_Transfer
- Leave its Category to Value and its Variable Type to CString.
- Click OK.
- In the Message Maps property sheet, double-click IDC_BTN_TRANSFER.
- Set its function to OnTransfer and click OK.
- While the function is still selected, click Edit Code.
- Implement the function as follows:
void CListOfCountriesDlg::OnTransfer()
{
m_ListCountries.GetText(m_ListCountries.GetCurSel(), m_Transfer);
UpdateData(FALSE);
}
Add the Content of an Edit Box to a List Box
- On the dialog, add a button identified as IDC_BTN_ADD
- Set its Caption to << Add
- On the right side of the previous button, add an Edit Box identified as IDC_EDIT_ADD
- Right-click anywhere on the dialog and click ClassWizard.
- Click the Member Variables property sheet.
- Double-click IDC_EDIT_ADD
- Set the name of the variable to m_Add
- Make sure its Category is Value and its Variable Type is CString.
- Click OK
- On the Message Maps property sheet, click IDC_BTN_ADD.
- In the Messages list, double-click BTN_CLICKED.
- Set its function OnAddToList
- Click OK and click Edit Code.
- Implement the function as follows:
void CListOfCountriesDlg::OnAddToList()
{
UpdateData();
m_ListCountries.AddString(m_Add);
UpdateData(FALSE);
}
Add an Item to a Specific Position in the List
- On the dialog, add a button named IDC_BTN_INSERT3
- Set its Caption to Ins on 3rd
- On the right side of that button, add an Edit Box named IDC_EDIT_INS3
- On the main menu, click View -> ClassWizard
- In the Member Variables property sheet, double-click IDC_EDIT_INS3
- Set the variable's name to m_Insert3
- Keep its Category to Value and its Variable Type to CString
- On the Message Maps property sheet, double-click the BTN_CLICKED of the
IDC_BTN_INSERT3
- Accept the name of the function by clicking OK and click Edit Code.
- Implement the function as follows:
void CListOfCountriesDlg::OnBtnInsert3()
{
UpdateData();
m_ListCountries.InsertString(2, m_Insert3);
UpdateData(FALSE);
}
Insert an Item above the Selected Item on the List
- On the dialog, add a button with a name of IDC_BTN_INSERT and a Caption of
Insert/Sel
- On the right side of that button, add an Edit Box named IDC_EDIT_INSERT
- Press Ctrl + W.
- On the Member Variables property sheet, double-click IDC_EDIT_INSERT
- Set its name to m_InsertItem and click OK
- On the Message Maps property sheet, add a BTN_CLICKED function for the
IDC_BTN_INSERT button
- Set the name of the function to OnInsertItem and click Edit Code.
- Implement the function as follows:
void CListOfCountriesDlg::OnInsertItem()
{
UpdateData();
m_ListCountries.InsertString(m_ListCountries.GetCurSel(), m_InsertItem);
UpdateData(FALSE);
}
Delete an Item in the List Using its Position - Item No. 5
- On the dialog, add a button called IDC_BTN_DELETE
- Set its Caption to Delete Item
- Double-click the button to access its BTN_CLICKED funtion
- Set its name to OnDeleteItem and click OK.
- Implement the function as follows:
void CListOfCountriesDlg::OnDeleteItem()
{
m_ListCountries.DeleteString(4);
UpdateData(FALSE);
}
Delete the Selected Item on the List
Delete/Clear the Whole List
- On the dialog, add a button called IDC_BTN_CLEARLIST
- Double-click the button and change its function to OnClearWholeList
- Click OK and implement the function as follows:
void CListOfCountriesDlg::OnClearWholeList()
{
m_ListCountries.ResetContent();
}
Get the Number of Items in the List
- When deleting or inserting items in a list, you might want to know how
many items are in a list box.
On the dialog, under the list box, add a Static text captioned Number of Items:
- Add an Edit Box on the right side of the previous text.
- Set the name of the Edit Box to IDC_EDIT_COUNT
- Press Ctrl + W to access the ClassWizard.
- In the Member Variables property Sheet, double-click the IDC_EDIT_COUNT
control.
- Set the name of the variable to m_ListCount
- Make sure the Category is Value. Set the Variable Type int.
- Click OK.
- Click the Message Maps property sheet.
- In the Member Functions list, click OnInitDialog and click Edit Code.
- Just under the last AddString function, type
m_ListCount = m_ListCountries.GetCount();
UpdateData(FALSE);
- To update the count of items in the list box, you can use the
CListBox::GetCount() function as necessary. For example, change the
implementation of the OnInsertItem() function follows:
void CListOfCountriesDlg::OnInsertItem()
{
UpdateData();
m_ListCountries.InsertString(m_ListCountries.GetCurSel(), m_InsertItem);
m_ListCount = m_ListCountries.GetCount();
UpdateData(FALSE);
}