Results 1 to 7 of 7

Thread: [RESOLVED] List to List

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Resolved [RESOLVED] List to List

    Hey guys!

    I need your assistance.
    I want to move data from a listbox to another listbox.
    I have two forms (Form1 and Form2) and two listbox (List1, List2).
    List1 is in Form1 and List2 in Form2.
    I have added data to List1 using a loop (for-next). Now i would like to move ALL data from List1 to List2 which is in Form2. I have found a thread that show how to move data from list to list, but i have to select any item from list1 and move it to list2. I would like to do it automatically. Let's say, press a button and move ALL data from List1 to List2. How i can do that?

    Thanks,
    stratos

  2. #2
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: List to List

    You can do this..
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
       Dim I As Integer
          For I = 0 To Form1.List1.ListCount - 1
             Form1.List1.ListIndex = I
             Form2.List2.AddItem (Form1.List1.List(Form1.List1.ListIndex))
          Next I
    End Sub
    Edit: If you want this code fully automatic you can put it in a TimerControl.
    Last edited by CDRIVE; Sep 19th, 2010 at 08:41 AM. Reason: Make note and add reference to Form2
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  3. #3
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: List to List

    Something like this?
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
    4.     Dim I As Integer
    5.    
    6.     If (KeyCode = vbKeyReturn) Then
    7.         For I = 0 To (List1.ListCount - 1) Step 1
    8.             Call List2.AddItem(List1.List(I))
    9.         Next I
    10.     End If
    11. End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: List to List

    Thanks so much guys! I was typing a code similar to CDRIVE. It was working good (items have been added at list2), but at the end an error message was coming up and program crashed. ''invalid procedure etc''. Now it works perfect! Thanks again. I typed also two more lines of code to remove data from list1:
    --Remove ALL items from List1 of Form1
    For i = Form1.List1.ListCount - 1 To 0 Step -1
    Form1.List1.RemoveItem i
    Next i

    P.S. CDRIVE. I put the code at form_load, so when the form pops up, list is filled in.

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [RESOLVED] List to List

    If you have any code that might repeat the AddItem method you may want to add the Clear method as I did here. Without this line repeated clicks would cause List2 to not match List1. Since you have your code in your Form_Load event, it won't matter though.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
       Dim i As Integer
       '** insure that List2 always matches List1 by first clearing List2.   **
       If Form2.List2.ListCount <> 0 Then Form2.List2.Clear
         
          For i = 0 To Form1.List1.ListCount - 1
             Form1.List1.ListIndex = i
             Form2.List2.AddItem (Form1.List1.List(Form1.List1.ListIndex))
          Next i
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: List to List

    Quote Originally Posted by stratos View Post
    Thanks so much guys! I was typing a code similar to CDRIVE. It was working good (items have been added at list2), but at the end an error message was coming up and program crashed. ''invalid procedure etc''. Now it works perfect! Thanks again. I typed also two more lines of code to remove data from list1:
    --Remove ALL items from List1 of Form1
    For i = Form1.List1.ListCount - 1 To 0 Step -1
    Form1.List1.RemoveItem i
    Next i

    P.S. CDRIVE. I put the code at form_load, so when the form pops up, list is filled in.
    You can simplfy your code by using ..
    Code:
    Form1.List1.Clear
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  7. #7
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: [RESOLVED] List to List

    Code:
    Public Function MoveFromListToList(ByVal Index As Integer, ByRef FromList As Control, ByRef ToList As Control) As Long
    
    On Error GoTo errHandler
    
    ' Removes a selected item from one list and puts it in another list.
    ' Returns 0 if succesful or -1 if nothing moved.
      
    MoveFromListToList = -1
    
    If Index < 0 Then Exit Function ' Return -1
    If Not IsList(FromList) Then Exit Function ' Return -1
    If Not IsList(ToList) Then Exit Function  ' Return -1
    
    With ToList
      .AddItem FromList.List(Index)
      .ItemData(.NewIndex) = FromList.ItemData(Index)
    End With
    
    FromList.RemoveItem Index
    
    ' Return 0
    MoveFromListToList = 0
    
    Exit Function
    
    errHandler:
    
    MoveFromListToList = -1
    
    End Function
    
    Public Function IsList(List As Control) As Boolean
    
    ' Determines if control is a list.
    
    IsList = (TypeName(List) = "ListBox") Or (TypeName(List) = "ComboBox") Or (TypeName(List) = "FileListBox")
    
    End Function

Tags for this Thread

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