CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    161

    add items to a list view

    Hi,

    I am using the following code to create a List View Control:

    Code:
    	// Adding List-View Columns
    	TCHAR szText[256] = {0};
    	TCHAR szString[5][20] = { TEXT("Column 1"),TEXT("Column 2"),TEXT("Column 3") };
    
    	HWND hlistview = CreateWindowEx(0, WC_LISTVIEW, 0, WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_ALIGNLEFT | LVS_REPORT | LVS_EDITLABELS, 40,90,430,200, hwnd, (HMENU)IDC_LIST1, hInst, NULL);
    	SetWindowFont(hlistview,hfont0,TRUE);
    
    	LVCOLUMN lvc;
    	ListView_DeleteAllItems(hlistview);
    
    	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    
    	for (int k = 0; k < 3; k++) 
    	{
    		lvc.iSubItem = k;
    		lvc.cx       = 100;
    		lvc.pszText  = szString[k];
    		lvc.fmt      = LVCFMT_LEFT;
    
    		ListView_InsertColumn(hlistview, k, &lvc);
    	}
    
    	// Adding List-View Items and Subitems
    
    	struct petinfo
    	{
    		TCHAR szKind[10];
    		TCHAR szBreed[50];
    		TCHAR szPrice[20];
    	};
    
    	petinfo pi[] = 
    	{
    		{TEXT("Dog"), TEXT("Poodle"), TEXT("$300.00")},
    		{TEXT("Cat"), TEXT("Siamese"), TEXT("$100.00")},
    		{TEXT("Fish"), TEXT("Angel Fish"), TEXT("$10.00")}
    	};
    
    	LVITEM lvi;
    	lvi.mask      = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; 
    	lvi.state     = 0;
    	lvi.stateMask = 0;
    
    	for (int k = 0; k < 3; k++)
    	{
    		lvi.iItem    = k;
    		lvi.iSubItem = 0;
    		lvi.lParam   = (LPARAM) &pi[k];
    		int res = ListView_InsertItem(hlistview, &lvi);
    	}
    while I get the list view collumn correct I don't get any items...what am I doing wrong?

    thanks

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: add items to a list view

    Try it something like this instead:

    Code:
     LVITEM lvi;
      memset(&lvi,0,sizeof(LVITEM));
      lvi.mask      = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; 
      lvi.state     = 0;
      lvi.stateMask = 0;
    
      for (int k = 0; k < 3; k++)
      {
    
    
        lvi.iItem    = k;
        lvi.iSubItem = 0;
        //lvi.lParam   = (LPARAM) &pi[k];
        int res = ListView_InsertItem(hlistview, &lvi);
        ListView_SetItemText(hlistview,res,0,pi[k].szKind);
        ListView_SetItemText(hlistview,res,1,pi[k].szBreed);
        ListView_SetItemText(hlistview,res,2,pi[k].szPrice);
      }

  3. #3
    Join Date
    Dec 2009
    Posts
    161

    Re: add items to a list view

    Thank you ever so much!

    By the way, do yo think there's any way to add a line between the rows (not that important to me though)

  4. #4
    Join Date
    Dec 2009
    Posts
    161

    Re: add items to a list view

    Also, I ma having a lot of compiler errors because of this:

    Code:
    	struct petinfo
    	{
    		TCHAR szKind[10];
    		TCHAR szBreed[50];
    		TCHAR szPrice[20];
    	};
    
    	std::map<int, petinfo> rows;
    	
    	{
    		petinfo pinfo = { TEXT("Name1"), TEXT("Surname1"), TEXT("Age1") };
    		rows.insert( std::make_pair(0, pinfo) );
    	}
    
    	{
    		petinfo pinfo = { TEXT("Name2"), TEXT("Surname2"), TEXT("Age2") };
    		rows.insert( std::make_pair(1, pinfo) );
    	}
    
    	std::map<int, petinfo>::const_iterator itt;
    	
    	LVITEM lvi;
    	memset(&lvi,0,sizeof(LVITEM));
    	lvi.mask      = LVIF_TEXT | LVIF_STATE; 
    	lvi.state     = 0;
    	lvi.stateMask = 0;
    
    	for(itt=rows.begin(); itt!=rows.end(); ++itt)
    	{
    		lvi.iItem    = itt._Dec;
    		lvi.iSubItem = 0;
    
    		ListView_InsertItem(hlistview, &lvi);
    
    		ListView_SetItemText(hlistview, itt, 0, pinfo[itt].szKind);
    		ListView_SetItemText(hlistview, itt, 1, pinfo[itt].szBreed);
    		ListView_SetItemText(hlistview, itt, 2, pinfo[itt].szPrice);
    	}
    what am Idoing wrong?

    thanks

  5. #5
    Join Date
    Dec 2009
    Posts
    161

    Re: add items to a list view

    I am sorry that was my mistake!

    Code:
    	for(itt=rows.begin(); itt!=rows.end(); ++itt)
    	{
    		lvi.iItem    = itt->first;
    		lvi.iSubItem = 0;
    
    		ListView_InsertItem(hlistview, &lvi);
    
    		ListView_SetItemText(hlistview, itt->first, 0, (LPWSTR)itt->second.szBreed);
    		ListView_SetItemText(hlistview, itt->first, 1, (LPWSTR)itt->second.szKind);
    		ListView_SetItemText(hlistview, itt->first, 2, (LPWSTR)itt->second.szPrice);
    
    	}
    Last edited by THEARTOFWEB; February 13th, 2010 at 08:50 PM.

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: add items to a list view

    I'm not sure what the problem is(was) but you should be consistent with your string macros. If you use the _T() or TEXT() macros, then you should use the tstring versions everywhere:

    Code:
    		ListView_SetItemText(hlistview, itt->first, 0, (LPTSTR)itt->second.szBreed);
    		ListView_SetItemText(hlistview, itt->first, 1, (LPTSTR)itt->second.szKind);
    		ListView_SetItemText(hlistview, itt->first, 2, (LPTSTR)itt->second.szPrice);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured