CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 1999
    Posts
    3

    Move ListItem from 1 ListView to another



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

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Move ListItem from 1 ListView to another



    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



  3. #3
    Join Date
    Feb 1999
    Posts
    3

    Re: Move ListItem from 1 ListView to another



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

  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Move ListItem from 1 ListView to another



    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured