I have an MFC List Control in Visual Studio 2013 (C++) with a List of items (Report view)
LVCOLUMN lvColumn;
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
lvColumn.pszText = "Full Name";
((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(0, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 75;
lvColumn.pszText = "Profession";
((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(1, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 80;
lvColumn.pszText = "Fav Sport";
((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(2, &lvColumn);
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 75;
lvColumn.pszText = "Hobby";
((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertColumn(3, &lvColumn);
LVITEM lvItem;
int nItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
lvItem.pszText = "Sandra C. Anschwitz";
nItem = ((CListCtrl*)GetDlgItem(IDC_LIST1))->InsertItem(&lvItem);
((CListCtrl*)GetDlgItem(IDC_LIST1))->SetItemText(nItem, 1, "Singer");
((CListCtrl*)GetDlgItem(IDC_LIST1))->SetItemText(nItem, 2, "HandBall");
((CListCtrl*)GetDlgItem(IDC_LIST1))->SetItemText(nItem, 3, "Beach");
How can I have multiline items for Full Name, Profession, Sport and Hobby?
Surprisingly, this is not possible with the default CListCtrl. But, with a little custom coding (and some trickery), you can get the effect you want.
First, you’ll need to derive your own class from CListCtrl and set the owner draw bit (Owner Draw Fixed = true) for the control style. In your parent dialog class, create an image list (here’s the trickery). The image list will be used to specify the height of each row of the list control. In my example below, I used:
You’ll need to play around with the cx and cy values for the image list to fit your needs. Your control will use the image list to size each row because it’s anticipating displaying icons. Next, add a handler for DrawItem like this:
In my example, this results in…
It may not be an elegant solution, but, it works. Note: With this approach, every row will have the same height.
EDIT: There are a few ways to obtain the row text. The easiest would be to use GetItemText like so:
The above assumes you set the text for each row using one of the CListCtrl methods to set text.