Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,916 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,110 people online right now. Registration is fast and FREE... Join Now!




listview checked items

 
Reply to this topicStart new topic

listview checked items, how to move checked items from 1 listview to another

bolson50
10 Jan, 2008 - 08:57 PM
Post #1

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 15


My Contributions
hello i am trying to move checked items from listview7 to listview1 on another form so fat this is the best i have been able to do. i have taken this from a vb6 program that i know works and made what changes i could. the 2 problem peices of the code are

1.)If ListView7.CheckedItems.Item(j) = True Then
error
Error 1
Overload resolution failed because no accessible 'Item' can be called without a narrowing conversion:
'Public Overridable ReadOnly Default Property Item(key As String) As System.Windows.Forms.ListViewItem': Argument matching parameter 'key' narrows from 'Long' to 'String'.
'Public ReadOnly Default Property Item(index As Integer) As System.Windows.Forms.ListViewItem': Argument matching parameter 'index' narrows from 'Long' to 'Integer'.

2.) Dim new_item As New frmEmail.ListViewItem(("ListView7.SelectedItems").ToString)
error Error 2
Type 'frmEmail.ListViewItem' is not defined.


the whole code chunk is listed below that i am trying to use
thank you in advance for any and all your help

CODE

Private Sub ListView7_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView7.SelectedIndexChanged        
Dim j As Long

        For j = 1 To ListView7.SelectedItems.Count
            If [u]ListView7.CheckedItems.Item(j)[/u] = True Then
                'if selected then add to other list
                Dim new_item As New[u] frmEmail.ListViewItem[/u](("ListView7.SelectedItems").ToString)
                'etc to include all subitems so the entire row is moved

            End If
        Next j

        frmEmail.Show()
end sub

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Listview Checked Items
10 Jan, 2008 - 11:54 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Here is the basic idea. You read through the collection of checked items using checkedindices. This will give you index values of the checked items. You then need to clone each item before moving it (because you can't have the same item in two listboxes). Then move it and remove it from the first listview.

There is a trick to this though, when you remove an item, it shortens the list each time but doesn't adjust the checkedindices so you setup a counter to offset this fact. Here is the code example...

CODE

Dim j As Integer
Dim myform2 As New Form2
Dim lvwItem As ListViewItem

Dim count As Integer = 0

' Loop through checked items in Listview7 on form 1
For Each j In ListView7.CheckedIndices
     ' Clone the item first (make a copy before moving)
     lvwItem = ListView7.Items(j - count).Clone()

     ' Remove from original listview on form 1 using index
     ListView7.Items.Remove(ListView7.Items(j - count))

     ' Place in listview1 on form 2
     myform2.ListView1.Items.Add(lvwItem)

     count += 1
Next

' Show the form
myform2.Show()


So play with this a little and make your tweaks to it. Enjoy!

"At DIC we be listview manipulating code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

bolson50
RE: Listview Checked Items
11 Jan, 2008 - 03:43 PM
Post #3

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 15


My Contributions
thanks Marty it works great i just took out the Dim myform2 As New Form2 all together and replaced
the from name i wanted to send the information to. and i see that if you do not remove a checked item
that it repeats that item. and again thank you for your help.

QUOTE(Martyr2 @ 11 Jan, 2008 - 12:54 AM) *

Here is the basic idea. You read through the collection of checked items using checkedindices. This will give you index values of the checked items. You then need to clone each item before moving it (because you can't have the same item in two listboxes). Then move it and remove it from the first listview.

There is a trick to this though, when you remove an item, it shortens the list each time but doesn't adjust the checkedindices so you setup a counter to offset this fact. Here is the code example...

CODE

Dim j As Integer
Dim myform2 As New Form2
Dim lvwItem As ListViewItem

Dim count As Integer = 0

' Loop through checked items in Listview7 on form 1
For Each j In ListView7.CheckedIndices
     ' Clone the item first (make a copy before moving)
     lvwItem = ListView7.Items(j - count).Clone()

     ' Remove from original listview on form 1 using index
     ListView7.Items.Remove(ListView7.Items(j - count))

     ' Place in listview1 on form 2
     myform2.ListView1.Items.Add(lvwItem)

     count += 1
Next

' Show the form
myform2.Show()


So play with this a little and make your tweaks to it. Enjoy!

"At DIC we be listview manipulating code ninjas!" decap.gif


User is offlineProfile CardPM
+Quote Post

buffdaemon_live
RE: Listview Checked Items
4 Aug, 2008 - 12:21 PM
Post #4

New D.I.C Head
*

Joined: 4 Aug, 2008
Posts: 2

I found it very very Helpful. Before i had a problem doing this with selectedindices. Many many thanks to Martyr2. Works great

This post has been edited by buffdaemon_live: 4 Aug, 2008 - 12:23 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month