PRB: Delete All Columns in the List Control (198232)
The information in this article applies to:
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q198232 SYMPTOMS
When you attempt to delete columns from the List control, you might receive
unexpected results. In particular, trying to delete all the columns in the
List control might not work.
CAUSE
Because of the internal design of the List control, this problem can occur
when you delete all the columns from a List control through a forward
iterating loop. For example, when you delete the column at index 0,
Column(0), the indices of all of the remaining columns decrement by one
(for example, Column(1) becomes Column(0)).
The following sample code does not delete all of the columns:
CListCtrl *m_pList = NULL;
m_pList = (CListCtrl*) GetDlgItem(IDC_LIST1);
int nColumns = 10;
for ( int i = 0; i < nColumns; i++)
m_pList->DeleteColumn (i);
In the first pass of the loop, Column(0) is deleted and all the column
indices decrement by one. As a result, Column(1) becomes Column(0) in the
first pass of the loop. In the second pass of loop, the value of i is 1, so
Column(1) is deleted and the loop never gets to Column(0).
RESOLUTION
If you want to delete all the columns in a loop, you need to delete the
columns in the reverse order in which they were added. So you need to have
a decrementing loop instead of an incrementing loop. If the total number
of columns is ten (0-9) in the List control, you need to start deleting
from the last column, Column(9), instead of the first, Column(0), as in the
following example:
CListCtrl *m_pList = NULL;
m_pList = (CListCtrl*) GetDlgItem(IDC_LIST1);
int nColumns = 10;
for ( int i = nColumns-1; i >= 0; i--)
m_pList->DeleteColumn (i);
STATUS
This behavior is by design.
REFERENCES
LISTHDR MFC sample in VC++ Help.
CListCtrl documentation in MSDN.
Modification Type: | Major | Last Reviewed: | 12/11/2003 |
---|
Keywords: | kbcode kbCtrl kbprb KB198232 |
---|
|