Comparing listboxes and moving items

KoyoteGambit

New member
Joined
May 14, 2008
Messages
4
Location
Jamaica
Programming Experience
3-5
Hey all, I have a problem with a program I'm working on; I have two list boxes that lists files in a directory, Local and Remote. I want to check if the items in the Remote Directory are in the Local Directory and if not to copy the Remote directory files not found to the Local Directory. I'm able to list the files and all but I can't seem to be able to copy the files not found. This is what I have so far;

For k As Integer = ListBoxRemote.Items.Count - 1 To 0 Step -1
If ListBoxLocal.Items.Contains(ListBoxRemote.Items(k)) Then
MessageBox.Show("Item Found")
Else
MessageBox.Show("Item Not Found")
Dim fi As New FileInfo("RemoteDirectoryLocation" & ListBoxRemote.Items(k))
Dim overwrite As Boolean = True
fi.CopyTo("LocalDirectoryLocation" & ListBoxLocal.Items(k), overwrite)
End If
Next
What am I doing wrong, or whats the correct method please?
 
Not sure if this will help I just added two listboxes to a form and button slip this code in and play around with it a bit I think this is what you are trying to accomplish and it should work.

VB.NET:
Public Class Form1



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("1")
        ListBox1.Items.Add("2")
        ListBox1.Items.Add("3")
        ListBox1.Items.Add("4")

        ListBox2.Items.Add("3")
        ListBox2.Items.Add("4")
        ListBox2.Items.Add("5")
        ListBox2.Items.Add("6")
        ListBox2.Items.Add("7")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objects As Object()
        Dim intCounter As Integer = -1
        For Each obj As Object In ListBox1.Items
            If ListBox2.Items.Contains(obj) Then
                MsgBox(obj.ToString & " exists already.")
            Else
                intCounter += 1
                ReDim Preserve objects(intCounter)
                objects(intCounter) = obj

            End If
        Next
        ListBox2.Items.AddRange(objects)
    End Sub
End Class
 
I'm going to try this and see if it works, to be honest I have a bit of a hard time understanding this code, but I'll try to break it down later and have a go at it. Thanks again.
 
VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'populate the listboxes
ListBox1.Items.Add("1")
        ListBox1.Items.Add("2")
        ListBox1.Items.Add("3")
        ListBox1.Items.Add("4")

        ListBox2.Items.Add("3")
        ListBox2.Items.Add("4")
        ListBox2.Items.Add("5")
        ListBox2.Items.Add("6")
        ListBox2.Items.Add("7")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'array to hold items on first listbox that need to be moved
        Dim objects As Object()
'counter to keep track of the index in the array
        Dim intCounter As Integer = -1
'loop through each item in the first listbox
        For Each obj As Object In ListBox1.Items
'check if it is in the second listbox
            If ListBox2.Items.Contains(obj) Then
'yes it does
                MsgBox(obj.ToString & " exists already.")
            Else
'no it doesnt
                intCounter += 1
'redimension and keep the conents of the array of the contents we need to move
                ReDim Preserve objects(intCounter)
'add this object to the array of ones we need to move
                objects(intCounter) = obj

            End If
        Next
'add the array of object we need to move onto the other listbox
        ListBox2.Items.AddRange(objects)
    End Sub
 
Back
Top