Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals
Creating a class based on CListView only makes the list control available. You must still
initialize it, specify the necessary style(s) or extended style(s), and configure it as you see
fit using the apprpriate events.
The best place to initialize the list view is probably the CListView::OnInitialUpdate()
event. Here is an example:
void CExoLVView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl& lCtrl = GetListCtrl();
}
Once you have a reference to the ClistCtrl that controls the view, you can use everything
else we covered earlier with regards to the list control. For example, to create columns,
call any of the CListCtrl::InsertColumn() methods. To create the items that compose
the list, call any of the CListCtrl::InsertItem() methods and use the
CListCtrl::SetItemText() method to create sub items. The bitmaps or icons are used in
the same way.
Practical Learning: Configuring a List View
- To create columns for the list view and display it in report mode, change the
OnInitialUpdate() event of the view class as follows:
void CCountriesView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
CListCtrl& lCtrl = GetListCtrl();
lCtrl.InsertColumn(0, "Name", LVCFMT_LEFT, 120);
lCtrl.InsertColumn(1, "Area km\262", LVCFMT_CENTER, 80);
lCtrl.InsertColumn(2, "Population", LVCFMT_LEFT, 120);
lCtrl.InsertColumn(3, "Capital", LVCFMT_LEFT, 100);
lCtrl.InsertColumn(4, "Code", LVCFMT_CENTER, 40);
lCtrl.InsertColumn(5, "Independance", LVCFMT_CENTER, 88);
lCtrl.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
ModifyStyle(0, LVS_REPORT);
}
- Test the application: