Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls
// Fill out the Sample Fields list and select the first item
for(int j = 0; j < SizeContacts; j++)
m_SampleFields.AddString(Contacts[j]);
m_SampleFields.SetCurSel(0);
}
void CTableWizardDlg::OnSelectPersonal()
{
// TODO: Add your control notification handler code here
// Empty the lists
m_SampleTables.ResetContent();
m_SampleFields.ResetContent();
// Fill out the list
for(int i = 0; i < SizePersonal; i++)
m_SampleTables.AddString(Personal[i]);
// Select the first item in the list
m_SampleTables.SetCurSel(0);
// Fill out the Sample Fields list and select the first item
for(int j = 0; j < SizeAddresses; j++)
m_SampleFields.AddString(Addresses[j]);
m_SampleFields.SetCurSel(0);
}
- Test your program and return to MSVC
20.1.4..List Box Messages and Events..........................................................
In order to user the list box as a control, it must first receive focus. This can be visible by
either the first item having a rectangle drawn around it or at least on the items being
selected. When the list box receives focus, it sends the LBN_SETFOCUS notification
message. On the other hand, once the user clicks another control or another application,
the list box loses focus and consequently sends the LBN_KILLFOCUS notification
message.
As clicking is the most performed action on a list box, when the user clicks an item, it
becomes selected. If the user clicks another item of a non-Multiple Selection list box, as
the selection is going to be changed, the LBN_SELCHANGE notification message is
sent. If the selection is cancelled, the LBN_SELCANCEL notification message is sent.
If the user double-clicks an item in the list box, the LBN_DBLCLK notification message
is sent.
Practical Learning: Exploring List Boxes
- When the user clicks a table in the Sample Tables list box, we must change the
content of the Sample Fields list box and fill it out with the fields that are part of the
selected table.
Right-click the IDC_SAMPLE_TABLES list box and click Add Event Handler - In the Messages Type list, click LBN_SELCHANGE and, in the Class List, make
sure CTableWizardDlg is selected