Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Remember Listview Checkbox state between updates


Reply
 
Thread Tools Display Modes
  #1  
Old 08-15-2012, 10:33 PM
Jason88 Jason88 is offline
Junior Contributor
 
Join Date: Oct 2005
Posts: 301
Default Remember Listview Checkbox state between updates


I'm extracting names from a webpage every 30 minutes and showing them in a Listview. Sometimes names are added to or deleted from the webpage.

Currently I clear the Listview first and then add all the names. This way the Listview always shows all the names from the webpage, no matter how many names have been added or deleted between updates.

My problem now is that I need to add Checkboxes to the Listview, but the Checkbox status of each ListviewItem must be remembered between updates, so I can't simply clear the entire Listview.

Does anybody know how to handle this? New names added to the Listview are checked by default. I don't need help with getting the names from the webpage, I just need some way to remember the checked state of each name between updates. The names are in a List(Of String) and there are never duplicates.


Let's say we have the following names in the Listview with a certain checked state:

[x] Alex
[ ] Brian
[x] John
[x] Maria
[ ] Susan
[x] Susie

On the next update one name is deleted from the webpage (Alex) and two names are added (Chris, Patricia).

The Listview should now look like this:

[ ] Brian
[x] Chris
[x] John
[x] Maria
[x] Patricia
[ ] Susan
[x] Susie
Reply With Quote
  #2  
Old 08-16-2012, 03:59 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,285
Default

One way would be to have a collection of checked items that you put together before clearing the list, then using that to update the listview after editing the items.

Code:
Dim checked as new collection
dim lvi as listViewItem

' Work though each item in the listview
For each lvi in MyListView.Items
    ' Check if it's checked
    If lvi.Checked Then
        ' If it is checked then it gets added to the collection, the main thing is adding the
        ' text as the key in the collection because then you can use Contains on the
        ' collection to see if something exists rather than searching through the whole
        ' collection. The key must be unique but you've already said that's taken care of
        ' so it shouldn't be an issue.
        checked.Add(lvi.Text, lvi.Text)
    End if
Next

' At this point the listview items get changed...

' After the listview has changed, work through the listview again to check anything
' that needs checking
for each lvi in MyListView.Items
    ' See if the item is in the collection of checked items
    if checked.contains(lvi.Text) then
        ' This item was checked before so check it again
        lvi.Checked = True
    end if
next
Another way of going about it would be instead of storing a List(Of String) you could store a list of your own class, where your own class would contain the Name (which was originally stored in the list) but also has a boolean Checked property that has its last checkbox state.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 08-16-2012, 04:51 AM
Jason88 Jason88 is offline
Junior Contributor
 
Join Date: Oct 2005
Posts: 301
Default

Thank you very much. That works great.

I have one more little problem, but I doubt there's a solution for this. When I scroll halfway down the Listview and update it, then the scrollbar automatically jumps back to the top again, because the entire Listview gets repopulated. Is there way to keep the scrollbar at the same position?
Reply With Quote
  #4  
Old 08-16-2012, 05:05 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,285
Default

It gets difficult because the items are likely to change, so there's no guarantee that returning to where you were will be the same place.

But the ListView has a TopItem property that gives you the first ListViewItem that is currently in view. And ListViewItems have a EnsureVisible method you can use to make the listview scroll to a particular item.

So you could store the text of the TopItem and then EnsureVisible on that item, but there is no guarantee that item is still going to exist.

Or you could store the Index of the TopItem and then EnsureVisible on that Index, but there is no guarantee that the same number of items are going to exist.

It's doable, but tricky.

MSDN article for TopItem that also discusses EnsureVisible -> http://msdn.microsoft.com/en-us/libr...w.topitem.aspx
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 08-16-2012, 05:29 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,285
Default

It would possibly be simpler not to clear the contents of the listview every time.

Is there are particular reason you do that?

You get the list of names, you check to see if each name exists in the listview and if it doesn't you add it. You then check to see if every listview item exists in the list of names and if it doesn't you remove it.

All checkboxes would stay the same and it should leave the scroll pretty much where it was, I think.
__________________
There are no computers in heaven!
Reply With Quote
  #6  
Old 08-16-2012, 06:21 AM
Jason88 Jason88 is offline
Junior Contributor
 
Join Date: Oct 2005
Posts: 301
Default

Only adding and removing the required names keeps the scrollbar where it is. Thank you for your help. This is resolved.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->