Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't populate column 2 of list control 1

Status
Not open for further replies.

Captrick458

Programmer
Mar 27, 2005
37
US
I have a list control that I have set up a column 2 with a header. Everything works just fine except that the data for column 2 doesn't populate. No errors, just not there.

Any Ideas????

iSubItem is set to 1.

Thanks, Rick
 
Code:
// Assuming ctrl is a CListCtrl
int index = ctrl.InsertItem(ctrl.GetItemCount(), "Row1Col1");
ctrl.SetItemText(index,1,"Row1Col2");


/Per
[sub]
www.perfnurt.se[/sub]
 
/Per

Thanks, worked like a charm.

I am wondering why using the LVITEM structure wouldn't work. I had set mask to LVIF_TEXT and also ORed it with LVIF_IMAGE. Set the iItem to the line number, and iSubItem to 1. Also, pszText was set to an appropriate character array. I know that I have this working elsewhere, so there is either a subtle difference somewhere, or I have been lucky with previous uses that some random bit was set, etc. I sure would like to know what is going on.

If you have any ideas, I would appreciate hearing them.

Thanks again.
 
Can you post the code in question?

/Per
[sub]
www.perfnurt.se[/sub]
 
This was part of a large class, and included some SQL queries, so, for testing purposes, I simply created a dialog app, and placed this code in the OnOK() function, and commented out the exit code.
Code:
	LVCOLUMN lvc;
	LVITEM lvi;
	int ii,blar;
	int count;
	char itembuff[51],itembuff1[51];
	memset(&lvi,0,sizeof(lvi));
	memset(&lvc,0,sizeof(lvc));
	lvc.mask = LVCF_TEXT|LVCF_WIDTH;
	lvi.mask = /*LVIF_IMAGE|*/LVIF_TEXT;
//ABOVE LINE TRIED WITH AND WITHOUT LVIF_IMAGE
	lvc.cx=130;
	lvc.pszText = "User Name";
	m_ctlList1.InsertColumn(0,&lvc);
	lvc.cx=150;
	lvc.pszText = "Default Type";
	m_ctlList1.InsertColumn(1,&lvc);
	m_ctlList1.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	m_ctlList1.DeleteAllItems();

	CString SQL1,name1,def1,test;

	UpdateData(FALSE);
	strcpy(itembuff,"AAAAAA");
	strcpy(itembuff1,"aaaaaa");
	m_ctlList1.ShowWindow(SW_SHOW);
	for(count=1;count<10;count++)
	{
		lvi.iItem = count;
		lvi.pszText = itembuff;
		lvi.iSubItem=0;
		blar=m_ctlList1.InsertItem(&lvi);
		lvi.pszText = itembuff1;
		lvi.iSubItem=1;
//		m_ctlList1.SetItemText(blar,1,itembuff1);
//              THE ABOVE LINE WORKS GREAT, THANKS TO PER
//              THE BELOW LINE WILL NOT WORK-RETURNS 0
		m_ctlList1.SetItem(&lvi);
		itembuff[0]++;
		itembuff1[0]++;
	}
	UpdateData(FALSE);
Thanks again, Rick
 
I believe it is because you do SetItem on an index that doesn't exist.

Code:
lvi.iItem = count;
You start count from 1 => iItem == 1 but the real index (which you call blar) starts from 0.

The major difference why SetItemText works is that
Code:
m_ctlList1.SetItemText(blar,1,itembuff1);
uses the correct index returned by InsertItem (blar).

I suggest
1) You let SetItem do the same.
2) Start iteration on count=0. It is common practice even since the old C days :)


/Per
[sub]
www.perfnurt.se[/sub]
 
You are absolutely correct. I can't explain starting at '1' other than a brain dump. I also can't believe that I looked at this code for hours and couldn't see the problem.

Oh well, a second, or in this case, third set of eyes.

Anyway, thanks a lot.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top