Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals
For example, to delete the second item of the list, pass a 1 value to this method. If you
want to delete all items of the control, call the CListBox::ResetContent() method. ITs
syntax is:
void ResetContent( );
This method simply dismisses the whole content of the list box.
At any time, if you want to know the number of items that a list box holds, call the
CListBox::GetCount() method whose syntax is:
int GetCount() const;
This method simply returns a count of the items in the list.
Once a list has been created, you and your users can use its items. For example, to select
an item, the user clicks it. To programmatically select an item, you can call the
CListBox::SetCurSel() method. Its syntax is:
int SetCurSel(int nSelect);
The nSelect argument specifies the item to select. To select the fifth item from a list box,
you can pass the nSelect argument with a value of 4
void CExoListBox1View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
...
m_ListOfStudents.SetCurSel(4);
}
If an item is selected in the list and you want to find out which one, you can call the
CListBox::GetCurSel() method. Its syntax is:
int GetCurSel() const;
Practical Learning: Using List Box Methods
- Change the OnInitDialog() member function as follows:
BOOL CTableWizardDlg::OnInitDialog()
{
CDialog::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
// Select the Business radio button
m_SelectBusiness.SetCheck(1);