CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33

Thread: ListCtrl in MFC

  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,617

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    GCDEF, Could you bit elaborate, on how to use SetItemData in this case ?
    Have you read what SetItemData does? It stores an associated DWORD value with a list control item. If you see an item that has text in the second column store that row number somewhere and use it for the value for SetItemData for subsequent items you insert. When you need to get the second column text, use GetItemData to get the item number that has the second column text to retrieve the text.

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,461

    Re: ListCtrl in MFC

    Here's the order of evolution:
    1) win 32
    2) MFC
    3) something else

    Here's the order of evolution for an MFC list control:
    1) items copied into the list control
    2) setitemdata/getitemdata (better than 1 previous).
    3) virtual list control, essentially binds a list control to a list. Better than all the rest.

    Search for Arjay and virtual listcontrol in the forum. I bet I have code examples from 10 years ago.

  3. #18
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    @Arjay: Thanks a lot

    I'll check for the tutorials..

    As GCDEF mentioned, i need to use setitemdata/getitemdata. Still working on that, not solved yet. Its taking time for me as this is new area, but slowly getting there ! , with all help and inputs here

  4. #19
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    Arjay,

    I went through one of your examples, but couldnot get anything from that.

    I am looking for some simple code, so i can relate and learn. May be expand to practical applications later.

    It will also be helpful if i get suggestions for MFC c++ books, for beginners. I want to initially learn simple stuff.

    Also the microsoft documentation is not helpful at all, at least for beginners without any MFC knowledge

    thanks

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,440

    Re: ListCtrl in MFC

    For MFC, the 'bible' was 'Programming Windows with MFC' 2nd Edition by Jeff Prosise (1337 pages!). It's quite an old book (1999) but covers the basics. There are other's available (and newer) but I can't comment.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.0)

  6. #21
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    Thankyou very much kaud

  7. #22
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    Looks like I understood the idea behind the SetItemData and GetItem:

    Changed my code to following , but i guess, now i can build my code upon this:

    Code:
    BOOL CMFCApplication2Dlg::OnInitDialog()
    {
    
    
    	// TODO: Add extra initialization here
    
    	priya_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
    
    	int nItem = priya_list.InsertColumn(0, _T("Cell"), LVCFMT_LEFT, -1, 0);
    	priya_list.InsertColumn(1, _T("Site"), LVCFMT_LEFT, -1, 0);
    
    	int centralsite;
    	vector<int> sites;
    	bool bIsfirstCentralsite(false);
    	for (const auto pair : m)
    	{
    		bIsfirstCentralsite = true;
    		string centreId;
    		int index = pair.first;
    		if (index == 1)
    		{
    			centreId = "first";
    		}
    		else
    		{
    			centreId = "second";
    		}
    
    		CString ccetre(centreId.c_str());
    		for (const auto site : pair.second)
    		{
    			int nItem(0);
    			if (bIsfirstCentralsite)
    				nItem = priya_list.InsertItem(priya_list.GetItemCount(), ccetre);
    			else
    				nItem = priya_list.InsertItem(priya_list.GetItemCount(), _T(""));
    
    			auto itr = n.find(site);
    			string sSiteId = itr->second;
    			CString cSiteId(sSiteId.c_str());
    			priya_list.SetItemText(nItem, 1, cSiteId);
    
    			if (centreId == "first")
    			{
    				priya_list.SetItemData(nItem, (DWORD)(1));
    			}
    			else
    				priya_list.SetItemData(nItem, (DWORD)(2));
    
    			bIsfirstCentralsite = false;
    
    		}
    	}
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    
    void CMFCApplication2Dlg::OnBnClickedAddToSiteDB()
    {
    
    	CListCtrl * priya_ctrl = (CListCtrl *) GetDlgItem(IDC_LIST1);
    
    	POSITION pos = priya_ctrl->GetFirstSelectedItemPosition();
    
    		int nItem = priya_ctrl->GetNextSelectedItem(pos);
    
    		int centreid = (int)priya_ctrl->GetItemData(nItem);
    
                   // derive the centre id string from this number
    
    }

  8. #23
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    But still not able to understand the virtual list ctrl

  9. #24
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,937

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    But still not able to understand the virtual list ctrl
    It is much more complicated (especially for beginners) than the trivial list control. Try to find some sample code that will help you to understand it.

    Note also that virtual list control make sense if your list contains more than a couple hundred of items.
    Victor Nijegorodov

  10. #25
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    Thankyou Victor. Google doesnot have some good examples (at least for beginners), so i can get the basic concept.

    If the MFC experts in the forum, find some spare time and help me comeup with solution with virtual list, will be great help for me .
    Or even if come with other simple example will help me a lot !

  11. #26
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,461

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    But still not able to understand the virtual list ctrl
    See my response in this thread. The lvselstate.,zip attachment is a sample project of a virtual list control.
    https://forums.codeguru.com/showthre...01#post1919601

  12. #27
    Join Date
    May 2015
    Posts
    431

    Re: ListCtrl in MFC

    Thanks a lot Arjay.

    I tried to load the project with my free version of visual studio 2015. (where i have created some simple sample MFC projects yesterday). But i am getting lot of errors , when trying to build and run this project

    Name:  MFC.jpg
Views: 22
Size:  23.8 KB

  13. #28
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,461

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    Thanks a lot Arjay.

    I tried to load the project with my free version of visual studio 2015. (where i have created some simple sample MFC projects yesterday). But i am getting lot of errors , when trying to build and run this project

    Name:  MFC.jpg
Views: 22
Size:  23.8 KB
    I wrote this sample 16 years ago with VC++ 7.1 so I'm not surprised it doesn't compile. I'll look into updating it to Visual Studio 2019 Community Edition.

  14. #29
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,461

    Re: ListCtrl in MFC

    I've updated the project to VS2019 (sorry I don't have VS2015 installed, and the 2019 community edition is free, so why not use the latest?).

    For something that was written 16 years ago, upgrading it to 2019 was painless, I only had to change the WINVER from 0x0500 to 0x0501.

    This was originally an x86 ANSI character set VC 7.1 app, but when it converted to VC2019, it build and ran as UNICODE app. Ordinarily, this would have caused a lot of upgrade headaches, but there were no issues because I used the _T("") macros for string literals throughout (had I simply used "my string" literals, I would have had work to do getting it to compile).

    To compile this, you'll need the v142 build tools and MFC v142 libraries.
    1) Open the Visual Studio 2019 Installer
    2) Select 'Modify'
    3) Click on the Individual Components tab
    4) Under Compilers, build tools and runtimes, select MSVC v142 - VS 2019 C++ x64/x86 build tools
    5) Under SDKs, libraries, and frameworks, select C++ v14.2x MFC for v142 build tools (x86 & x64)

    Note: there are multiple versions for v14.2x for step 4 and 5 in each category. I don't know which one is being used and am too lazy to track it down, so I just selected them all.

    This sample looks dated and it uses images to display a checkbox rather than using the images built into the list control (which may or may not been available 16 years ago). That was around the XP timeframe and they looked okay back then with the standard Windows theme, but they sure are ugly now.

    There is a lot going on in this sample:
    1) Uses a virtual list control
    a) Uses a callback (via an LVN_GETDISPINFO handler) to retrieve the row data from a list
    b) Unlike a regular list control, it doesn't initially get populated by copying row data into the control
    c) Virtual list controls can handle large data sets (especially when used with a list that is a memory mapped file).
    d) Virtual list controls are very responsive. Try scrolling a virtual list control with 10K items, vs. a regular list control with 10K items - the regular list control will be dog slow
    2) Uses a background thread to populate the list control
    a) The secondary thread doesn't block the UI thread while filling the list
    b) The UI remains responsive while the background operations are performed.
    3) While this sample does not show this, the background thread can change the list data, and the virtual list control will pick the changes up without needing to do anything (kind of an early form of control binding).
    4) Shows list control row 'hit testing'. Because images are used, we are using ON_NOTIFY, NM_CLICK to check if the user has clicked on the icon that represents the check box. If the 'check box' icon has been clicked on we toggle the check state in the underlying list item, and tell the control to update the item (which causes the LVN_GETDISPINFO callback to be called).
    Attached Files Attached Files

  15. #30
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,617

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    Looks like I understood the idea behind the SetItemData and GetItem:

    Changed my code to following , but i guess, now i can build my code upon this:

    Code:
    BOOL CMFCApplication2Dlg::OnInitDialog()
    {
    
    
    	// TODO: Add extra initialization here
    
    	priya_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
    
    	int nItem = priya_list.InsertColumn(0, _T("Cell"), LVCFMT_LEFT, -1, 0);
    	priya_list.InsertColumn(1, _T("Site"), LVCFMT_LEFT, -1, 0);
    
    	int centralsite;
    	vector<int> sites;
    	bool bIsfirstCentralsite(false);
    	for (const auto pair : m)
    	{
    		bIsfirstCentralsite = true;
    		string centreId;
    		int index = pair.first;
    		if (index == 1)
    		{
    			centreId = "first";
    		}
    		else
    		{
    			centreId = "second";
    		}
    
    		CString ccetre(centreId.c_str());
    		for (const auto site : pair.second)
    		{
    			int nItem(0);
    			if (bIsfirstCentralsite)
    				nItem = priya_list.InsertItem(priya_list.GetItemCount(), ccetre);
    			else
    				nItem = priya_list.InsertItem(priya_list.GetItemCount(), _T(""));
    
    			auto itr = n.find(site);
    			string sSiteId = itr->second;
    			CString cSiteId(sSiteId.c_str());
    			priya_list.SetItemText(nItem, 1, cSiteId);
    
    			if (centreId == "first")
    			{
    				priya_list.SetItemData(nItem, (DWORD)(1));
    			}
    			else
    				priya_list.SetItemData(nItem, (DWORD)(2));
    
    			bIsfirstCentralsite = false;
    
    		}
    	}
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    
    void CMFCApplication2Dlg::OnBnClickedAddToSiteDB()
    {
    
    	CListCtrl * priya_ctrl = (CListCtrl *) GetDlgItem(IDC_LIST1);
    
    	POSITION pos = priya_ctrl->GetFirstSelectedItemPosition();
    
    		int nItem = priya_ctrl->GetNextSelectedItem(pos);
    
    		int centreid = (int)priya_ctrl->GetItemData(nItem);
    
                   // derive the centre id string from this number
    
    }
    I don't think you did understand. SetItemData should be an item number, not a hard coded value.

    As to the virtual list control, it's been a while for me, but I remember that as being a way to speed up performance of a list control with a lot of data. I'm not seeing how it helps with the OP's problem.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)