Click to See Complete Forum and Search --> : Move ListItem from 1 ListView to another


Angus Ching
February 22nd, 1999, 10:14 AM
Hi,


I'm using 2 buttons to control the movement of ListItems from 1 ListView to

another. I would like to know how it is done.


Private Sub cmdAdd_Click()



Dim itmListItem As ListItem

Dim temp1, temp2 As String



With ListView2

Set itmListItem = ListView1.SelectedItem.ListSubItems(0)

itmListItem = ListView1.SelectedItem.ListSubItems(1)

temp1 = itmListItem.SubItems(0)

temp2 = itmListItem.SubItems(1)



End With



MsgBox "temp1 = " & temp1 & " temp2 = " & temp2



End Sub


The error message is 'Index out of bounds'. What's wrong with the above code??

Chris Eastwood
February 22nd, 1999, 11:21 AM
Hi


I think you need to be a bit more specific - on which line do you get the error ?


Some other possible problems :


Dim temp1, temp2 As String


This line will create temp1 as a variant, and temp2 as a string


Set itmListItem = ListView1.SelectedItem.ListSubItems(0)

itmListItem = ListView1.SelectedItem.ListSubItems(1)

temp1 = itmListItem.SubItems(0)


This code seems a little unclear - In VB5 there is no property called ListSubItems - I presume you mean SubItems.


SubItems(0) is an invalid Reference in VB5 - this could be the cause of your error.


Regards


Chris Eastwood


CodeGuru - the website for developers

http://www.codeguru.com/vb

Angus Ching
February 22nd, 1999, 07:44 PM
>Dim temp1, temp2 As String


All this while, I thought the above declare temp1 and temp2 as strings.

Thanks for pointing that out.

How about 'Dim temp1 as string, temp2 as string'?? Will this do??


The source codes in the earlier posting is not exactly correct because

I've no idea of how to move a ListItem from 1 ListView to another. Maybe you

can show me a set of simple source codes that moves a ListItem to a ListView.

Chris Eastwood
February 23rd, 1999, 04:20 AM
Hi


The 'Dim' statement always catches people out (Dim x as string, y as string is fine)


Here's some code I've just tested in VB5 - create a new project and add two listviews to form1 (listview1 and listview2), also add a command button (command1 - used for moving items from listview1 to listview2)


Option Explicit


Private Sub Command1_Click()

Dim li As ListItem



'

' Get Selected ListItem from ListView1

'

Set li = ListView1.SelectedItem



If li Is Nothing Then

'

' Nothing Selected

'

Exit Sub

End If



With ListView2.ListItems

.Add , li.Key, li.Text

End With

'

' Now remove it from ListView1 - comment or remove it if you like

'

ListView1.ListItems.Remove li.Index



End Sub


Private Sub Form_Load()

Dim li As ListItem

Dim lCount As Long

Dim sKey As String



ListView1.View = lvwReport

ListView1.HideSelection = False

ListView2.View = lvwReport

ListView2.HideSelection = False



With ListView1.ColumnHeaders

.Add , , "Column 1"

.Add , , "Column 2"

End With



With ListView2.ColumnHeaders

.Add , , "Column 1"

.Add , , "Column 2"

End With



With ListView1.ListItems

For lCount = 1 To 25

Set li = .Add(, "Key" & lCount, "Item " & lCount)

li.SubItems(1) = "Sub Item"

Next

End With

End Sub


Regards


Chris Eastwood


CodeGuru - the website for developers

http://www.codeguru.com/vb