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.
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.
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
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!
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.
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 !
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
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
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.
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).
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.