List View Sync

I am trying to keep two list views in sync.

Can anyone tell me what is wrong with the following code?

 lstvMotivationMini.Items(lstvActionListMini.SelectedItems(0).Index).Selected = True
 lstvMotivationMini.Select()

the following also fails????????
lstvMotivationMini.Items(3).Selected = True
lstvMotivationMini.Select()
aparfittAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

doobdaveCommented:
Hi,

To select an item, use the SelectedIndex property:

lstvMotivationMini.SelectedIndex = 3

Regards,

David
aparfittAuthor Commented:
there doesn't seem to be a SelectedIndex property?
Olaf_RabbachinCommented:
Haven't tried, but I would attempt to do so using something like this:

      lvYourCopy.SelectedItems.Clear()
      For Each li As ListViewItem In lvSource.SelectedItems
         lvYourCopy.Items(li.Index).Selected = True
      Next


Cheers & HTH,
Olaf
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

aparfittAuthor Commented:
sadly that doesn't work 8-(
eventprostrategiesCommented:
i'm guessing you just want to keep the selected index in sync?  it'd be possible to keep image indexes or the listviewitems themselves in sync ... tho, a bit more fun (more difficult) ... here's what i quick did to keep the selectedindex (selectedindices.item(0) ... because listviews don't have a single selectedindex property) in sync with n listviews.

note: ( to monitor the selected index, i put a textbox above each listview which displays the selected index of that listview ... cause you won't be able to see the items both selected ... as only one of the controls will hold focus and such )

also -- this only allows for one selected index ... but you could mod it easily to allow for more

private sub sync(lv as listview)

dim idx as integer = -1

if lv.selectedIndices.count > 0 then idx = lv.selectedindices.item(0)

if idx < 0 or idx > lv.items.count - 1 then exit sub

dim lstview as listview
dim ctl as control
for each ctl in me.controls
    if typeof (ctl) is listview then
     lstview = ctl
    if not lstview is lv then
    if idx <> -1 then
     deselectall(lstview)  '// just loops thru the listviewitems in the listview and sets .selected = false
     lstview.items(idx).selected = true
     end if '// <> -1
     end if '// not lstview is lv
     end if '// typeof ctl ...
next

me.textbox1.text = me.listview1.selectedindices.item(0)
me.textbox2.text = me.listview2.selectedindices.item(0) '// hardcoded at the moment, for testing ...

end sub

----------

listviews_selectedindexchanged(sender as object, e as eventargs)

dim lv as listview = ctype(sender, listview)
sync(lv)

exit sub

----------

then, on load ...

dim lv as listview
dim ctl as control
for each ctl in me.controls
if typeof (ctl) is listview
lv = ctl
addhandler lv.selectedindexchanged, addressof listviews_selectedindexchanged
end if
next

and ... that should work.  could be written better, but it was speed-coding.  hope it works for ya
amyhxuCommented:
Suppose the two listviews have the same number of items, the easiest way to sync would be:

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        For i As Integer = 0 To ListView1.Items.Count - 1
            ListView2.Items(i).Selected = ListView1.Items(i).Selected
        Next

    End Sub

    Private Sub ListView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged

        For i As Integer = 0 To ListView2.Items.Count - 1
            ListView1.Items(i).Selected = ListView2.Items(i).Selected
        Next

    End Sub


This would allow sync multiple selections. However, notice that if the listView doesn't have the focus, the selected items won't be highlighted, therefore you won't visually see the effect. But if you tab to the listview not on focus, you will see the same items are highlighted. Also, you can programmatically display the items selected in the listview not on focus, eg:
        For Each i As Integer In ListView2.SelectedIndices
            Console.WriteLine(i & " " & ListView2.Items(i).Text)
        Next

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eventprostrategiesCommented:
good call for multiple selections, amyhxu  :)

if you make the 2 methods 1  (Private Sub ListViews_SelectedIndexChanged ...) and ...

dim lv as ListView = CType(sender, ListView)

then For Each thru the other listviews on the form and if the lv <> the one the started the sync, then do the ...
 For i As Integer = 0 To ListView2.Items.Count - 1
            ListView1.Items(i).Selected = ListView2.Items(i).Selected
 Next

...

then you can handle a variable number of listviews And multiple selections  :P
amyhxuCommented:
Initially, I did think about writing one method or handling two listviews' selectedIndexChanged event together. But to demonstrate the idea clearly, plus aparfitt only need to deal with two listviews, I think this is the best way to show the idea and he can make whatever changes he wants once he gets it.

But thank you for the suggestion, eventprostrategies :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.