Solved

Remove an item from Virtual list of ClistCtrl.

Posted on 2006-07-03
15
1,040 Views
Last Modified: 2013-11-20
Hi,

I am using Virtual list of Clistctrl to get information very fast. but i am facing a problem, when i am going to delete an item using CListCtrl::DeleteItem(index), it deletes the item at given index, but row from virtual list does not decreement or delete. can any body sort out this?. how can i remove the row from a virtual list?

Thanks in advance.

Regards
-devender
0
Comment
Question by:devendersys
  • 7
  • 6
  • 2
15 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 50 total points
Comment Utility
If I remember correctly I have told you before a virtual list ONLY stores how many items it has.  You can't use DeleteItem(index) because no item is stored at index.

What you do is instruct the list how many items it has (reduce previous count by one) and then invalidate the list control (m_lstCtrl.Invalidate()).  The DrawItem handler you have will then be queried for displaying the 'new' contents.
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
Hi there,

A virtual List View Control sens LVN_GETDISPINFO messages to its owner for each of its items that need painting.
It is up to you to determine the actual item parameters based on the index provided through LVN_GETDISPINFO  (sorry, you probably knew that ;-)

I would really recommend using a CListCtrl derived class to implement the 'virtual' behaviour. Then you'd have access to a 32bit value associated with each (virtual) item in the list view. The use of this value is up to you, maybe a pointer to the actual item, maybe an index into a CArray or std::vector.

The natural choice to store the actual Items (structs, objects, whatever) is an array, totally independant of the List View visualization.
You don't want your data to 'live' in the List View, unless it's really primitve information, like a list of integers.

Most of the time, you already have the data in some kind of array already. Rather than really inserting it into the View, you add Items with a LPSTR_TEXTCALLBACK placeholder and said 32bit value of your choice(e.g. index in data array).

It is then your responsibility to provide item-content upon  LVN_GETDISPINFO  requests by the framework.
Removing or adding items does require deleting/adding both the item in the List View and the item in the data array. You may have to re-establish the relationships between the two, for indicees stored in the 32bit member of the List View Item may not be valid anymore.

If you delete an item in the List View alone:  no harm done - it simply isn't visible anymore.
If you delete an item  in the data array alone:  you will be asked via LVN_GETDISPINFO   to provide information on just that item and you will have to return 'nothing', leaving a ghost in the List View or you will (accidently !) reach for Item index 12345 in the data array, which may now out of bounds and crash your program.
Or it will now point to a completely different item .. well, you get the picture.

As I understand (have never used them) the virual CListView (not CListCtrl) does almost that, but you do not have very much control of how the actual item data is stored, let alone store it as you like and independent of the list view.

Maybe I off your topic, though.
Please let me know.

Cheers,
Sebastian



0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Sebastian - You don't manipulate items in a virtual list control, all you do is tell it how many there are and it calls back for more information with the index when it requires information.  That is why they are so fast to populate especially with large sets of data (If it has 50000 entries you don't add 50000 items, you just have one line of code - ListHasThisNumberOfItems(50000) - then it is up to you to provide the info for item at eg. index 23456)  Change the sorting of the data - simple, just Invalidate().  
The LPSTR_TEXTCALLBACK is for a 'normal' list control, but one in which the text isn't stored for the item.
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
OK ... but how do you identify the items ?
Is the index independent of sorting order ?


Cheers,
Sebastian
 
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
It is up to you to track the items (typically recordset, array or linked list type of storage structure).  The list control just has the total number of items to handle.  It doesn't care what the item is or how you store/sort it.

The list will call back for info about the item at index 1, 2, 3....999.....
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
Am I beeing blind....??
 
You don't care what order they have... You are defining the order on the fly, aren't you.
But doesn't that mean a lot of organizational overhead on your side. How would I know what item #23456 is supposed to be, unless I have sorted my internal array appropriately.
And actually sorting the Item data every time the user wishes to do so would be stupid.

*confused*
Sebastian
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
And the framework will  probably call back in a random access manner, depending on the painting requirements.
So how is this supposed to be faster, when you have to shuffle your data everytime the user blinks

No hard feelings - I know that I don't know anything;-)
Sebastian
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
No, you aren't being blind.
The virtual list control is pretty dumb BUT very efficient at what it does.  It is up to you to handle the data.  You have a tremendous performance hit to add 50000 items, especially if the user finds what they want on the first page displayed.

<And actually sorting the Item data every time the user wishes to do so would be stupid.>
If the user wants to display the data in another sort order how would you go about it without actually sorting the data (or pointers).  That applies to both a 'normal' list control and a virtual list control.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
<And the framework will  probably call back in a random access manner,>
Actually no, if there is only room for 15 lines to be displayed you will only be called back for 15 lines of data (in my experience in order as well).

Think of adding 50000 items to the list then displaying one page of data - lot of work.
Think of telling the list it has 50000 items then displaying one page of data - rather less work for the system.
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
oh well, I could sort only the array of pointers rather than the item data, which might be quite big.
That is assuming of course that the CListCtrl isn't sorting big chunks of item information. I donn't know much about its internals, unfortunately.

But I see the point of fast insertion and deletion of long lists. Watching the Listview as it removes items can be painful;-)

Cheers,
Sebastian
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Sorting - often sorting pointers is more efficient (less memory to move about).

You still might be missing something else.
If the list doesn't call back for an item you don't need to store anything about the item.  What if each of your 50000 items needed something (size of file on disk, volume of irregular object....) - not having to actually get that information can give you an even greater performance gain.
0
 
LVL 5

Expert Comment

by:bastibartel
Comment Utility
OK - that's a good point. So the items are even more 'virtual' with that approach.
0
 

Author Comment

by:devendersys
Comment Utility
hi guys,

My question was to delete an item from the list control if the items are stored in the virtual list. so as per the Andy's comments and some papers in MSDN. finally i got some solution.

Solution:

I am maintaining a STL list ( which is having a structure as object ) with List control. whenever i want to display data in the list control i just set CListCtrl::SetItemCount(No. of Items) and map my STL list with particular row in OnGetDispInfo(...) function. now when i want to delete one or more item(s) from the list, i just erase the items from STL list and call again SetItemCount(...) with refreshed STL list.

Is there any better solution than this? ;)

Thanks Andy.

-Devender

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Better?  It should be very efficient and do what you require.
0
 

Author Comment

by:devendersys
Comment Utility
Thanks Andy :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to update Firmware and Bios in Dell Equalogic PS6000 Arrays and Hard Disks firmware update.
Are you looking to recover an email message or a contact you just deleted mistakenly? Or you are searching for a contact that you erased from your MS Outlook ‘Contacts’ folder and now realized that it was important.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now