Home

List Box

 
List Box Exercise

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

Creating the Application

  1. Start Microsoft Visual C++
  2. On the main menu, click File -> New.
  3. Decide to create a Dialog Based application called ListOfCountries with a title as
    List of Countries
  4. While designing the dialog box, place a List Box control on it. Change the identifier of the  list box to IDC_LIST_COUNTRIES
  5. Place an Edit Box on the dialog. Call it IDC_LISTDBLCLK
  6. Right-click the List Box and click ClassWizard...
  7. Click the Member Variables property sheet.
  8. Double-click IDC_LISTCOUNTRIES to Add a Variable
  9. Set the Member Variable Name to m_ListCountries
  10. In the Category combo box, select Control

    Add Member Variable
     
  11. Click OK
  12. 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

  1. Press Ctrl + W to access the ClassWizard.
  2. Click the Message Maps property sheet. In the Class Name combo box, make sure CListExoDlg is selected. In the Object IDs, click IDC_LISTCOUNTRIES
  3. In the Messages list, double-click LBN_DBLCLK. Change the name of the function to OnDblclkList and click OK.
  4. 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.

  1. On the dialog, add a button identified as IDC_BTN_TRANSFER
  2. Set its Caption to >>
  3. On its right side, add an Edit Box identified as IDC_EDIT_TRANSFER
  4. Press Ctrl + W to access the ClassWizard.
  5. In the Member Variables property sheet, double-click IDC_EDIT_TRANSFER
  6. Set the name of the variable as m_Transfer
  7. Leave its Category to Value and its Variable Type to CString.
  8. Click OK.
  9. In the Message Maps property sheet, double-click IDC_BTN_TRANSFER.
  10. Set its function to OnTransfer and click OK.
  11. While the function is still selected, click Edit Code.
  12. 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

  1. On the dialog, add a button identified as IDC_BTN_ADD
  2. Set its Caption to << Add
  3. On the right side of the previous button, add an Edit Box identified as IDC_EDIT_ADD
  4. Right-click anywhere on the dialog and click ClassWizard.
  5. Click the Member Variables property sheet.
  6. Double-click IDC_EDIT_ADD
  7. Set the name of the variable to m_Add
  8. Make sure its Category is Value and its Variable Type is CString.
  9. Click OK
  10. On the Message Maps property sheet, click IDC_BTN_ADD.
  11. In the Messages list, double-click BTN_CLICKED.
  12. Set its function OnAddToList
  13. Click OK and click Edit Code.
  14. 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

  1. On the dialog, add a button named IDC_BTN_INSERT3
  2. Set its Caption to Ins on 3rd
  3. On the right side of that button, add an Edit Box named IDC_EDIT_INS3
  4. On the main menu, click View -> ClassWizard
  5. In the Member Variables property sheet, double-click IDC_EDIT_INS3
  6. Set the variable's name to m_Insert3
  7. Keep its Category to Value and its Variable Type to CString
  8. On the Message Maps property sheet, double-click the BTN_CLICKED of the IDC_BTN_INSERT3
  9. Accept the name of the function by clicking OK and click Edit Code.
  10. 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

  1. On the dialog, add a button with a name of IDC_BTN_INSERT and a Caption of Insert/Sel
  2. On the right side of that button, add an Edit Box named IDC_EDIT_INSERT
  3. Press Ctrl + W.
  4. On the Member Variables property sheet, double-click IDC_EDIT_INSERT
  5. Set its name to m_InsertItem and click OK
  6. On the Message Maps property sheet, add a BTN_CLICKED function for the IDC_BTN_INSERT button
  7. Set the name of the function to OnInsertItem and click Edit Code.
  8. 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

  1. On the dialog, add a button called IDC_BTN_DELETE
  2. Set its Caption to Delete Item
  3. Double-click the button to access its BTN_CLICKED funtion
  4. Set its name to OnDeleteItem and click OK.
  5. 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

  1. On the dialog, add a button called IDC_BTN_CLEARLIST
  2. Double-click the button and change its function to OnClearWholeList
  3. Click OK and implement the function as follows:
    void CListOfCountriesDlg::OnClearWholeList() 
    {
    	m_ListCountries.ResetContent();
    }

Get the Number of Items in the List

  1. 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:
  2. Add an Edit Box on the right side of the previous text.
  3. Set the name of the Edit Box to IDC_EDIT_COUNT
  4. Press Ctrl + W to access the ClassWizard.
  5. In the Member Variables property Sheet, double-click the IDC_EDIT_COUNT control.
  6. Set the name of the variable to m_ListCount
  7. Make sure the Category is Value. Set the Variable Type int.
  8. Click OK.
  9. Click the Message Maps property sheet.
  10. In the Member Functions list, click OnInitDialog and click Edit Code.
  11. Just under the last AddString function, type
    m_ListCount = m_ListCountries.GetCount();
    UpdateData(FALSE);
  12. 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);
    }
 

Home Copyright © 2001-2005 FunctionX