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

How i can insert items to a ListControl control? 1

Status
Not open for further replies.

vlitzer

Programmer
Apr 30, 2002
31
AR
What i need is to know how it works the ListControl. At least how to add new items and to insert colums.

Thanks anyway!
 
1) Call InsertColumn(0-based position,"Column title",ALIGNMENT_CONSTANT,intWidth) for each column you want to add. (This only applies if you're using Report View.)

Repeat
2) Use the InsertItem() function to insert an item into the control. The data you pass in is inserted into the first column. Make sure you store the return value, because you'll need it to add data into the other columns.

3) Use SetItemText() to add text into other columns.

Here's an example:

Code:
int itemPos, x;
CString data;

listCtrl.InsertColumn(0,"Column 1",LVCFMT_LEFT,100);
listCtrl.InsertColumn(1,"Column 2",LVCFMT_LEFT,100);
listCtrl.InsertColumn(2,"Column 3",LVCFMT_LEFT,100);

for (x=0;x<3;x++) {
    data.Format(&quot;(%d,1)&quot;,x);
    itemPos=listCtrl.InsertItem(x,data);
    data.Format(&quot;(%d,2)&quot;,x);
    listCtrl.SetItemText(itemPos,1,data);
    data.Format(&quot;(%d,3)&quot;,x);
    listCtrl.SetItemText(itemPos,2,data);
}

This code yields a table that looks like this:

Code:
Column 1|Column 2|Column 3
---------------------------
(1,1)   |(1,2)   |(1,3)
(2,1)   |(2,2)   |(2,3)
(3,1)   |(3,2)   |(3,3)
 
dds82: you dont know how your post help me!!!

thanks so much!!!! its what i need :)

thanks again!
 
other thing:

how i can implement that when i click one column they sort the list using the subitems of the column? like on the details of the Windows Explorer. Ive already find the ClickColumn event... but i dont know how to sort it, without doing manually.

thanks again anyway!
 
CListCtrl has a member function called SortItems() that you can call. The prototype is this:

Code:
int SortItems(PFNLVCOMPARE CompareProc, LPARAM lParamSort);

You call this function to sort the list using the function you pass in as CompareProc. A call to this function looks like this:

Code:
myList.SortItems((PFNLVCOMPARE)MyCompareFunc, myOptionalParam);

The CompareProc function must be declared like this:

Code:
int CALLBACK CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

It can be a class member function; I did this and had no problems. The parameters to CompareProc are this:

lParam1, lParam2: the data associated with the 2 items being compared. This data is set with the function CListCtrl::SetItemData(). (Yes, you MUST set some sort of data for each item if you want to be able to sort them properly.)

lParamSort: the 2nd parameter you passed in to SortItems().

Your CompareProc should return the following values:

If lParam1 == lParam2 (or whatever data you compare based on those parameters), return 0.
If lParam1 < lParam2, return a negative number.
If lParam1 > lParam2, return a positive number.

Good Luck!
 
Well, ive analized the information you provide me the last time, but there are

some things that i dont understand :(. Well first of all i dont get the main

idea:

1) I must read every item of the list to compare?

2) How can i assign the lparam1 & lparam2 value? The list.SetItemData() returns

a BOOL value.

3) The listControl control event OnColumnclickMainlist(NMHDR* pNMHDR, LRESULT*

pResult) have a lot of parameters that i dont understand. They seems to not to

change their integer value, no matters what column i click... So how i know What

column ive click?

4) The compareProc function, is that simple? here is the function that ive

created following your instruccions

int CALLBACK CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
if(lParam1 == lParam2)
return 0;
if(lParam1 < lParam2)
return -1;
else
return 1;
}

5) How i retrieve the subitems value?

well sorry if im a fool but i simply dont understand how it works. Again, thanks anyway :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top