Introduction
The "report-style" list control is commonly used in various situations, although we can directly use the MFC CListCtrl
class and specify it with LVS_REPORT
style, but if you ask me, that's just not enough.
First of all, it's sort of inconvenient to use, especially when you need to move a particular item up or down, or swap two items, or do something like "invert selection", you will have to write quite some lines of code. Also, CListCtrl
member method SetItemText
only accepts string values, which could be burdensome since you must convert every data into string first.
Second, CListCtrl
lacks of some necessary features which it really should have had. Item sorting by clicking on column headers, for instance, is always a must in most report-style list controls, yet we need to write our own code, quite complicated code, to achieve it. Another example is "item data", we often use heap pointers as item data in order to associate the list items with some real structures. In that case, it is crucial to remember to cleanup before deletion of any item, because we may not have a chance after an item is deleted. Wouldn't it be nice if there is a call back function which is automatically called when an item is about to be deleted from the list control, regardless of which and when?
Alright, let's take a look at what we can have. CReportCtrl
, is an MFC CListCtrl
derived class specialized in the report-style list control manipulation. A bunch of methods are implemented or overloaded in this class in order to provide fast, efficient and convenient access and operations. I say this class is all about convenience.
Features
It is no more necessary to convert all other data types into character strings before using SetItemText
because this method has been overloaded for all common data types, with precision of float/double numbers being taken into account.
Using heap pointers as list item data has become safer since the user defined CALLBACK
function BOOL (*) (DWORD, LPARAM)
will be called every time before a list item is actually deleted. Therefore you can perform the cleanup period to the deletion of each item, or abort the deletion anytime during the process of DeleteAllItems
if you feel things go wrong.
Item sorting is implemented, the comparing function will figure out how to compare your items according to how the particular sub item texts look like, being those in numeric, string, or date time formats. Your end user simply clicks on list header columns and all items will be sorted in desired manner.
Item selection/unselection, checking/unchecking, and repositioning are extensively implemented for sheer convenience.
Public methods
The CALLBACK function
typedef BOOL (CALLBACK *ITEMDATAPROC)(DWORD, LPARAM);
Column attributes & operations
int GetColumnCount() const;
BOOL SetHeadings(UINT uiStringID);
BOOL SetHeadings(const CString& strHeadings);
int InsertColumn(int nCol, const LVCOLUMN* pColumn);
int InsertColumn(int nCol, LPCTSTR lpszColumnHeading,
int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1);
BOOL DeleteColumn(int nCol);
BOOL DeleteAllColumns();
Item attributes & operations
void SetGridLines(BOOL bSet = TRUE);
void SetFullRowSelect(BOOL bSet = TRUE);
void SetCheckboxes(BOOL bSet = TRUE);
BOOL IsItemSelected(int nItem) const;
int GetSelectedItemCount() const;
int GetUnselectedItemCount() const;
int GetFirstSelectedItem(int nStartAfter = -1) const;
int GetFirstUnselectedItem(int nStartAfter = -1) const;
BOOL SelectItem(int nItem,
BOOL bSelectAdjacentIfNotAvailable = FALSE);
BOOL UnSelectItem(int nItem);
BOOL SelectAllItems();
BOOL UnSelectAllItems();
BOOL InvertSelect();
BOOL IsItemChecked(int nItem) const;
int GetCheckedItemCount() const;
int GetUncheckedItemCount() const;
int GetFirstCheckedItem(int nStartAfter = -1) const;
int GetFirstUncheckedItem(int nStartAfter = -1) const;
BOOL CheckItem(int nItem);
BOOL UnCheckItem(int nItem);
void CheckAllItems();
void UnCheckAllItems();
void InvertCheck();
int InsertItem(UINT nMask, int nItem,
LPCTSTR lpszItem, UINT nState,
UINT nStateMask, int nImage, LPARAM lParam);
int InsertItem(int nItem, LPCTSTR lpszItem, int nImage);
int InsertItem(const LVITEM* pItem);
int InsertItem(int nIndex, LPCTSTR pszText, ...);
BOOL DeleteItem(int iItem, BOOL bSelectNextItem = FALSE,
ITEMDATAPROC lpFunc = NULL,
LPARAM lParam = NULL);
int DeleteAllItems(ITEMDATAPROC lpFunc = NULL,
LPARAM lParam = NULL);
int DeleteAllSelectedItems(ITEMDATAPROC lpFunc = NULL,
LPARAM lParam = NULL);
int DeleteAllUnselectedItems(ITEMDATAPROC lpFunc = NULL,
LPARAM lParam = NULL);
int DeleteAllCheckedItems(ITEMDATAPROC lpFunc = NULL,
LPARAM lParam = NULL);
int DeleteAllUncheckedItems(ITEMDATAPROC lpFunc = NULL,
LPARAM lParam = NULL);
BOOL SwapItems(int nItem1, int nItem2);
BOOL MoveUp(int nItem, int nCount = 1);
BOOL MoveDown(int nItem, int nCount = 1);
BOOL MoveToTop(int nItem);
BOOL MoveToBottom(int nItem);
BOOL SetItemText(int nItem,
int nSubItem, LPCTSTR lpszText);
BOOL SetItemText(int nItem, int nSubItem,
DOUBLE val, int nPrecision = -1);
BOOL SetItemText(int nItem, int nSubItem,
FLOAT val, int nPrecision = -1);