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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CListCtrl::SetItemText() funtion doesn't work

Status
Not open for further replies.

kaya17

Programmer
Feb 9, 2004
78
SG
hi, everyone,
i have a problem with the SetItemText() funtion.
i have a CListCtrl m_listctrl,
and i have button.

what i want is that when the button is pressed, i want to insert some cstring to the 2nd column of the list.

i inserted the following code in the button funtion:

m_listctrl.SetItemText(0,1,"string");

but nothing comes out in the list.

would anyone help me with it?

really thank you !

regards,
kaya
 
I also has an initdialog() function to initialize the list.

ListView_SetExtendedListViewStyle(m_listctrl,LVS_EX_GRIDLINES);

m_listctrl.InsertColumn(0,"DELETE",LVCFMT_LEFT,-1,0);
m_listctrl.InsertColumn(1,"FROM",LVCFMT_LEFT,-1,0);
m_listctrl.InsertColumn(2,"WHERE",LVCFMT_LEFT,-1,0);

m_listctrl.SetColumnWidth(0,200);
m_listctrl.SetColumnWidth(1,200);
m_listctrl.SetColumnWidth(2,200);
 
I kinda had the same problem when I first tried to use the CListCtrl. Here's what I do.

for the very first column, use 'InsertItem'. I made myself a function to populate a listview from a recordset and according to which column it is determines which method I use. An example follows.

int column = 1;

for(int i = 0; i < nRecordCount; i++)
{

int ret = (column == 1 ? lst.InsertItem(i ,"column 1") : lst.SetItemText(i,column - 1,"Not column 1")) ;
}

So it seems you have to do an InsertItem first on every row, then you can use SetItemText on that same row as much as you want, even to change the first column.

I'm not very good at explaining things sometimes, if that doesn't help (I wrote it from scratch instead of copying and pasting it like I should have), let me know and I'll post something better



bdiamond
 
>it seems you have to do an InsertItem first on every row

Well, since SetItemText doesn't insert any item (thats what InsertItem does) you obviously have to first do an InsertItem then SetItemText.

I usually do it like this (roughly):
Code:
// Precondition: Apropriate headers have been inserted
for (int i=0; i< someLimit; ++i)
{
  int index = theList.InsertItem(i, "Some col 0 text");
  theList.SetItemText(index, 1, "Some col 1 text");
  theList.SetItemText(index, 2, "Some col 2 text");
  // etc...
}


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Thanks bdiamond and Perfnurt! bdiamond you've explained it well, though i think Perfnurt's code is easier to understand than yours for a beginner like me :p
Really thanks a lot for your guys' help!

P.S:i think they should state it clearly in the MSDN :(


Regards,
kaya

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top