|
-
Sep 19th, 2010, 05:57 AM
#1
Thread Starter
Member
[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
-
Sep 19th, 2010, 08:20 AM
#2
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?? 
-
Sep 19th, 2010, 08:22 AM
#3
Addicted Member
Re: List to List
Something like this?
vb Code:
Option Explicit Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer) Dim I As Integer If (KeyCode = vbKeyReturn) Then For I = 0 To (List1.ListCount - 1) Step 1 Call List2.AddItem(List1.List(I)) Next I End If End Sub
-
Sep 19th, 2010, 09:03 AM
#4
Thread Starter
Member
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.
-
Sep 19th, 2010, 09:49 AM
#5
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?? 
-
Sep 19th, 2010, 09:55 AM
#6
Re: List to List
 Originally Posted by stratos
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 ..
<--- 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?? 
-
Sep 19th, 2010, 10:38 AM
#7
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|