Welcome to Dream.In.Code
Become a VB Expert!

Join 137,261 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,549 people online right now. Registration is fast and FREE... Join Now!




copying values from a list box to another

 
Reply to this topicStart new topic

copying values from a list box to another, List Boxes

meagray22
26 Nov, 2006 - 09:14 PM
Post #1

New D.I.C Head
*

Joined: 26 Nov, 2006
Posts: 3


My Contributions
Here is my problem i'm working on this project: it contains three list's. What i need to accomplish is: find and move items that match in two of the lists and move the matching ones to the 3rd list box using a command button. A major problem for me is that i have no idea where to start. does this need to be If/else statme? loop? and how would i reference the objects.
CODE

Public Class Form1

    Private Sub RadSuppliers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadSuppliers.CheckedChanged

    End Sub

    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        'Add item to suppliers list
        If RadSuppliers.Checked = True Then
            RadProducers.Checked = False

            Me.Suppliers.Items.Add(textCompany.Text).ToString()
            Me.textCompany.Clear()

        Else : RadProducers.Checked = True
            'add item to producers list
            Me.Producers.Items.Add(textCompany.Text).ToString()
            Me.textCompany.Clear()

        End If
    End Sub

    Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
        'if values from suppliers and producers match then move items over to partners list

        
    End Sub

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Copying Values From A List Box To Another
26 Nov, 2006 - 10:05 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You can retrieve the values of what is stored in a listbox by looping through the list using the index. This is accessed through the Listbox.Items.Item(index) method, which returns the value of that position in the list.

Then you would need an IF statement to compare to the items in the other listbox, IF they are the same then copy to the third listbox or go on to the next item in the first listbox.

User is offlineProfile CardPM
+Quote Post

meagray22
RE: Copying Values From A List Box To Another
26 Nov, 2006 - 11:20 PM
Post #3

New D.I.C Head
*

Joined: 26 Nov, 2006
Posts: 3


My Contributions
If i've set my form so that the user can add the items to the first two list - how would i set an Index since they can change?


QUOTE(jayman9 @ 26 Nov, 2006 - 11:05 PM) *

You can retrieve the values of what is stored in a listbox by looping through the list using the index. This is accessed through the Listbox.Items.Item(index) method, which returns the value of that position in the list.

Then you would need an IF statement to compare to the items in the other listbox, IF they are the same then copy to the third listbox or go on to the next item in the first listbox.


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Copying Values From A List Box To Another
27 Nov, 2006 - 09:03 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
The index does not change.

Much like an array has an index number in order to access each element in the array, so does a listbox has an index that is determined by the number of items in the list.

The index will always start at 0 and will increment with each item in the list.

For example, here is a list of names, lets assume for the moment that they are stored in a listbox. There are a total of 4 items in the list and since the index starts at 0, then the index number of the last item in the list is 3.

Tom
Bill
Sammy
Jason

If I want to access the item that has an index of 0 in the list.
CODE

name = Listbox.Items.Item(0)

This will return the value of "Tom" and store it in the variable name.

If I want to access the item that has an index of 2 in the list.
CODE

name = Listbox.Items.Item(2)

This will return the value of "Sammy" and store it in the variable name.

Hopefully this will give you an idea of how it works.
User is offlineProfile CardPM
+Quote Post

meagray22
RE: Copying Values From A List Box To Another
27 Nov, 2006 - 09:06 PM
Post #5

New D.I.C Head
*

Joined: 26 Nov, 2006
Posts: 3


My Contributions
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
'Add item to suppliers list
If RadSuppliers.Checked Then


Me.Suppliers.Items.Add(textCompany.Text).ToString()
Me.textCompany.Clear()

Else
'add item to producers list
Me.Producers.Items.Add(textCompany.Text).ToString()
Me.textCompany.Clear()

End If
End Sub

Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
'if values from suppliers and producers match then move items over to partners list
'diclare variables
Dim suppliers As Boolean = False
Dim producers As Boolean = True

Do Until suppliers Or producers = Me.Partners.Items.Count
If Me.Partners.Items = Me.Suppliers.Items(producers).ToString() Then
suppliers = True
Else
producers += 1

End If
Loop
If suppliers = producers Then

'?????? i want it to appear in the partners list

End If

End Sub
' I dont know what to do to make that statement work - any suggestions?

User is offlineProfile CardPM
+Quote Post

m2s87
RE: Copying Values From A List Box To Another
28 Nov, 2006 - 05:36 AM
Post #6

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 1 times
Dream Kudos: 1225
My Contributions
I would make a makro that goes thru all list elements, if he finds 1 that is equal to 2, then he puts the item index number to the massive, that can later be used as a source of list 3 and deliting list of 1,2.

Public Sub analyys()
Dim i%, j%, k%
k = -1
ReDim m(1, k)

For Each element In Me.List1.items
i = i + 1

For Each element2 In Me.List2.items
j = j + 1

If element.items.item(i).Value = element2.items.item(j).Value Then
k = k + 1
ReDim m(1, k)
m(0, k) = i
m(1, k) = j
End If
Next element2
Next element
End Sub

This post has been edited by m2s87: 28 Nov, 2006 - 03:35 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 06:53PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month