Click to See Complete Forum and Search --> : Displaying numbers in ListView


cdalemax
November 17th, 2005, 03:21 AM
I am trying to display objects into a listview. Say my object is a "sold item". It contains the date sold (a struct of 3 ints), the name of the item and the price.

I use LV_ITEM and LVM_INSERTITEM to display my objects. pszText only works with text. How do I display the numbers and the structure of ints (the date)? Is there another way of doing things?

golanshahar
November 17th, 2005, 03:33 AM
you can convert the int data into string and pass the string.
look at this sample:

struct data{
int sold;
int price;
int name;
};
data d={1,2,3};
char szTxt[MAX_PATH]={0};
sprintf(szTxt,"name is %d price is %d sold %d",d.name,d.price,d.sold);
// pass szText to LVM_INSERTITEM



Cheers

humptydumpty
November 17th, 2005, 03:33 AM
it will acept number also.Simply use either _bstr_t or convert it to string .i Don't know how you are trying to do this.
here is a example. Not Filling structure by LV_ITEM
Create a ListControl with style report

LV_COLUMN listColumn,listColumn1;
listColumn.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
listColumn.fmt = LVCFMT_LEFT|LVCFMT_RIGHT;
listColumn.cx = 150;
listColumn.iSubItem = 0;
listColumn.pszText = "Name";
m_ListCtrl1.InsertColumn( 0, &listColumn ); //m_ListCtrl1 is the object of CListViewCtrl

listColumn1.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
listColumn1.fmt = LVCFMT_LEFT;
listColumn1.cx = 150;
listColumn1.iSubItem = 0;
listColumn1.pszText = "Phone Number";
m_ListCtrl1.InsertColumn( 1, &listColumn1 );


now put item in list control

for(int INewVal =0 ;INewVal <pContactArr1.GetSize() ;INewVal++)
{
m_ListCtrl1.InsertItem(INewVal, "Name");
m_ListCtrl1.SetItemText(INewVal,0,"Name");
m_ListCtrl1.SetItemText(INewVal,1,"Number");
}


Note

Setitem Text takes LPCTSTR lpszText (http://<font%20color=/) as it's last paramater so for converting your variable to specify type either you can take help of sprintf

or CString member function Format
like CString str;
str.Format(...);



Like :- MSDN Sample

CListCtrl* pmyListCtrl;
CString strText;
int nColumnCount = pmyListCtrl->GetHeaderCtrl()->GetItemCount();
// Insert 10 items in the list view control.
for (int i=0;i < 10;i++)
{
strText.Format(TEXT("item %d"), i);
// Insert the item, select every other item.
pmyListCtrl->InsertItem(
LVIF_TEXT|LVIF_STATE, i, strText,
(i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,
0, 0);
// Initialize the text of the subitems.
for (int j=1;j < nColumnCount;j++)
{
strText.Format(TEXT("sub-item %d %d"), i, j);
pmyListCtrl->SetItemText(i, j, strText);
}
}

NoHero
November 17th, 2005, 04:12 AM
it will acept number also.Simply use either _bstr_t or convert it to string .i Don't know how you are trying to do this.
here is a example. Not Filling structure by LV_ITEM
Create a ListControl with style report


Personally I don't like to see MFC code in the WinAPI section:


MFC code mostly simplify thinks which vary from the native API calls. So the OP might not be able to translate the MFC code back to WinAPI calls.
Most time the OP is not using MFC, of course he does not have expirience on OOP and/or is just a beginner. Posting MFC code - from he actually does not have any sense - might overload him.
And the purpose of the forum is to seperate pure WinAPI question from MFC like ones. Maybe a MFC code hijacks the thread or it goes offtopic from such posts above, because the may rise questions about the MFC itself -- and focuse on these rather to help the OP with his WinAPI problems.