Link to home
Start Free TrialLog in
Avatar of fstab
fstab

asked on

CListCtrl problem

I've created a MFC doc-view project which uses the CListView class. I'm using the CListView (and the underlying CListCtrl in effect) in Report View mode. I have added 3 columns to the CListCtrl using the following code :

CListCtrl &pList = GetListCtrl();
int width;

AfxGetMainWnd()->GetClientRect(&rect);
width = (rect.bottom - rect.top) /3;
if ((pList.InsertColumn(0,"SourceName", LVCFMT_LEFT,width)) == -1)
   AfxMessageBox("Could not insert column 0");
if ((pList.InsertColumn(1,"Computer Name",LVCFMT_LEFT,width,0)) == -1)
   AfxMessageBox("Could not insert column 1");
if ((pList.InsertColumn(2,"Event Type",LVCFMT_LEFT,width,1)) == -1)
   AfxMessageBox("Could not insert column 2");

After doing this I'm trying to insert items using InsertItem as follows :

for(countofitems = 0; countofitems < 3; countofitems++) {
item.mask = LVIF_TEXT;
item.iItem = countofitems;
if (countofitems == 0) {
   item.pszText = SourceName;
   item.iSubItem = 0;
} else if (countofitems == 1) {
   item.pszText = ComputerName;
   item.iSubItem = countofitems + 1;
} else if (countofitems == 2) {
   item.pszText = buf.GetBufferbuf.GetLength());
item.iSubItem = countofitems + 1;
}
pList.InsertItem(&item);
}

However, only the first column's data gets inserted (and is seen) not the next two (ie ComputerName and the variable buf).

Any pointers ?
fstab.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
???? width=(rect.bottom - rect.top)/3;

I think it should be:
width = (rect.right - rect.left)/3;
Avatar of MFC123
MFC123

Ensure the following order for item structure

fofirst row first column

item.iitem = 0; item.isubitem = 0;
insertitem

for  first row second column
item.iitem = 0; item.isubitem = 1;
setitem

for  first row second column
item.iitem = 0; item.isubitem = 2;
setitem






Avatar of fstab

ASKER

Thanks to all who responded. However, may I mention that I was also getting some other problems.

I noticed that though I used SetItemText, not all the subitems were getting set to the text I specified, and were random in nature.

I later discovered that this malady (correct me if I'm wrong anyone) was due to my setting the styles to :

cs.style |= LVS_REPORT | LVS_SINGLESEL | LVS_SORTASCENDING;

Don't see it ? Its due to the LVS_SORTASCENDING being set .. On removing this, all subitems were seen.

Thanks all again - esp. VBapat for telling me that teeny-weeny bit about the right - left !
fstab.