Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals



  1. Change its ID to IDC_SAMPLE_TABLES

  2. Remove the check box on the Sort property or set it to False

  3. Add a Control Variable for the IDC_SAMPLE_TABLES list box and name it
    m_SampleTables

  4. In the header file of the CTableWizardDlg class, declare two constant CString
    arrays as follows:


const CString Business[] = { "Contacts", "Customers", "Employees",
"Products", "Orders", "Suppliers",
"Payments", "Invoices","Projects",
"Events", "Transactions" };

const CString Personal[] = { "Addresses", "Video Collection",
"Authors", "Books", "Categories",
"Music Collection", "Investments" };


  1. At the end of the class, declare two private integer variables as follows:


private:
// Variables used to hold the number of items in their respective array
int SizeBusiness;
int SizePersonal;
};


  1. Save All


20.1.3..List Box Methods................................................................................


A list box is based on the CListBox class. Therefore, if you want to programmatically
create a list box, declare a CListBox variable or pointer using its constructor. To initialize
the control, call its Create() method. Here is an example:

void CExoListBox1View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();

CListBox *m_SimpleList = new CListBox;

m_SimpleList->Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL,
CRect(20, 20, 120, 120), this, 0x118);

After adding a list box, you can “fill” it up with items. This is done by calling the
CListBox::AddString() method. Its syntax is:

int AddString(LPCTSTR lpszItem);

This method expects a null-terminated string as argument and adds this argument to the
control. To add more items, you must call this method for each desired item. Here is an
example:

void CExoListBox1View::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
Free download pdf