Introduction
I would like to thank the authors of some old articles posted in CodeProject!! I have just consolidated information from them and created a reusable class, CListCtrlEx
. I hope you will enjoy using it.
Features
- Editable Cells � We can make any cell of the list control editable.
- Combo Box � We can place a combo box in any cell of the list control.
- Coloring � We can give different text colors as well as background colors for individual rows of the list control.
- Tooltip � Tooltip support is added with this class. Currently it shows only the current text in the cells.
Using the Code
Insert a list control into your dialog. Change the view style to Report. Make a member variable of type CListCtrlEx
, say m_ListCtrlMyList
.
Also your project should be configured to support the Unicode character set.
How to make a cell editable?
This class contains a public member function SetEditBox
.
void SetEditBox(int nItemIndex_i, int nSubItemIndex_i);
It accepts the item (row) number and the subitem (column) number as parameters.
Examples:
- If you want to make the fourth column of the third row editable, call:
m_ListCtrlMyList.SetEditBox(2, 3);
- If you want to make an entire row editable, call:
m_ListCtrlMyList.SetEditBox(2, -1);
It indicates to make all cells in the third row editable.
- If you want to make an entire column editable, call:
m_ListCtrlMyList.SetEditBox(-1, 3);
It indicates to make all cells in the fourth column editable.
How to place a combo box in a cell?
This class contains a public member function SetComboBox
.
void SetComboBox(int nRow_i, int nCol_i, CStringArray& csarValues_i);
It accepts the item (row) number and the subitem (column) number and a CStringArray
as parameters. The strings that should be displayed in the combo box should be filled in the CStringArray
before calling this function.
Examples:
CStringArray acsComboStrings;
acsComboStrings.Add(L"String1");
acsComboStrings.Add(L"String2");
acsComboStrings.Add(L"String3");
acsComboStrings.Add(L"String4");
m_ListCtrlMyList.SetComboBox(1,2, acsComboStrings);
As in the edit box, the following combinations are also supported.
m_ListCtrlMyList.SetComboBox(-1,-1, acsComboStrings);
m_ListCtrlMyList.SetComboBox(1,-2, acsComboStrings);
Note:
The combo box is not editable. If you want to edit it, right click on the cell in which the combo box is set. An edit box will be displayed.
How to change the text color and background of an item?
The text coloring is done within the OnCustomDraw
function. For assigning a particular color for a row, call the function AddColoredRow
:
void AddColoredRow(int nItemIndex,
COLORREF colTextColor = RGB(0, 0, 0),
COLORREF colBkColor = RGB(255, 255, 255));
It accepts the item (row) number and two COLORREF
objects for the text color and the background color.
Example:
m_ListCtrlMyList.AddColoredRow(3,
RGB(23,34,455),
RGB(56,23,45));
Note:
All the methods explained above should be used after inserting the necessary columns and items into the list control, if you want to see the correct results.