Reputation: 362
How to fix row and column repetition in CListctrl in MFC.
int nItem = 0;
m_list.InsertColumn(0, _T("Name"), LVCFMT_LEFT, 100);
m_list.InsertColumn(1, _T("Area km\262"), LVCFMT_RIGHT, 80);
m_list.InsertColumn(2, _T("Population"), LVCFMT_RIGHT, 100);
m_list.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
ModifyStyle(0, LVS_REPORT);
nItem = m_list.InsertItem(0, _T("Libya"), 8);
m_list.SetItemText(nItem, 1, _T("1,759,540"));
m_list.SetItemText(nItem, 2, _T("5,499,074"));
nItem = m_list.InsertItem(0, _T("Senegal"), 7);
m_list.SetItemText(nItem, 1, _T("196,190"));
m_list.SetItemText(nItem, 2,_T( "10,580,307"));
nItem = m_list.InsertItem(0, _T("Cuba"), 6);
m_list.SetItemText(nItem, 1, _T("110,860"));
m_list.SetItemText(nItem, 2, _T("11,263,429"));
Upvotes: 0
Views: 1071
Reputation: 10756
Is it not that you're simply not clearing the list before refreshing the contents. And it's appending rows and columns again rather than replacing. Call m_list.DeleteAllItems()
before refreshing.
Update after comment:
Right, so that confirms you're refreshing the contents too often or in the wrong place. DeleteAllItems()
will clear the contents, not remove column headers already added.
So either (1) call DeleteColumn()
on each of the already inserted columns first, or (2) preferably do the inserting of columns in a more appropriate place where it'll only happen once. Say in the OnInitDialog() of the dialog I presume you're working with.
Upvotes: 2
Reputation: 6566
CListCtrl
does not care if your item should be unique or not. So you have to handle it yourself.
You need to create and use std::map
or even std::set
structure for items that should be unique and do a lookup each time you need to insert an item or a column:
Insert:
std::set<CString> listItems;
listItems.insert(columnName);
Lookup:
const bool is_in = listItems.find(element) != listItems.end();
You can also use CMap
MFC container to do that.
Upvotes: 3