listview checked items

how to move checked items from 1 listview to another

Page 1 of 1

3 Replies - 29946 Views - Last Post: 04 August 2008 - 01:21 PM Rate Topic: -----

#1 bolson50  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-January 08

listview checked items

Post icon  Posted 10 January 2008 - 09:57 PM

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

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



Is This A Good Question/Topic? 0
  • +

Replies To: listview checked items

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: listview checked items

Posted 11 January 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...

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!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 bolson50  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-January 08

Re: listview checked items

Posted 11 January 2008 - 04:43 PM

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.

View PostMartyr2, on 11 Jan, 2008 - 12:54 AM, said:

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...

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!" :snap:

Was This Post Helpful? 0
  • +
  • -

#4 buffdaemon_live  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 08

Re: listview checked items

Posted 04 August 2008 - 01:21 PM

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: 04 August 2008 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1