Link to home
Start Free TrialLog in
Avatar of Philippe Renaud
Philippe RenaudFlag for Canada

asked on

How to dynamically concatenate strings from multiples ListBox Values

Hello EE,

I have this huge problem in my mind. Let me explain.
First of all, the form I have creates listBox dynamically based on a value in SQL server database on a table. Lets say the value is 4 well there will be 4 list Box on the form (I create those with a For x = 0 to "the value in db) thats No problem.

Also I populate the listboxes with different DB values. Still fine here.

Ok now the problem.  Lets say I have 3 list box like that:

       1st listBox                                  2nd List                                             3rd list

           01                                              aa                                                      Test
           02                                              bb                                                      Test2
           05                                               ff                                                       Test6

If the client select the row "01" and "02" and lets say also "aa" and "bb" and lets say for the third one "Test" and "Test2"

I need for result when he clicks on a button to show ALL the possible path of strings. For here the results (on a msgbox would be that) :

01aaTest
01aaTest2
01bbTest
01bbTest2
02aaTest
02aaTest2
02bbTest
02bbTest2

Ok I think I did not forgot one. Anyway you get the idea?  I know I need to create FOR for the number of listbox i have but the thing is I cannot know in advance the number of list box so I cannot Hard Code the For !!
I need a kind of Recursive thing and thats out of my scope.....

Any ideas ????? Thanks !!!!
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Something like this, I think
Dim result As New List(Of [String])()
For Each item As String In listBox1.Items
	For Each item2 As String In listBox2.Items
		For Each item3 As String In listBox3.Items
			result.Items.Add(String.format("{0}{1}{2}", item, item2, item3))
		Next
	Next
Next

Open in new window

Oops, did not read far enough :)
Let me get back to you .
Avatar of Philippe Renaud

ASKER

hahahah :)
By the way, i need the items that are Selected only. not all
You can use
Me.Forms.Controls.OfType(ListBox) to get all the listboxes in your form. Then you can use a foreach on all those listboxes, building your string(s) as you go along.
Would you mind write if you know it? I am so lost with all that
How do you name your listboxes when creating them dynamically?
I do :

For x As Integer = 0 To myRow("segmentCount")
                    Dim lstList As New ListBox
                    lstList.SelectionMode = SelectionMode.MultiExtended
                    lstList.Name = "lstSegment" + CStr(x)
                    lstList.Location = New Point(myX, 200)
                    lstList.Size = New Size(200, 200)
                   etc....
Next
You have to perform a sort of "Cartesian Product" between all selected items of each listbox.
Supposing to have an array of listboxes, the following code should do what you request:

    Dim ListBoxes() As ListBox

    Sub DoStuff()
        Dim Items As ICollection = ListBoxes(0).SelectedItems
        For i As Integer = 1 To ListBoxes.Length - 1
            Items = CartesianProduct(Items, ListBoxes(i).SelectedItems)
        Next
    End Sub
   
    Function CartesianProduct(ByVal set1 As ICollection, ByVal set2 As ICollection) As ICollection
        Dim resultList As New ArrayList
        For Each item1 In set1
            For Each item2 In set2
                resultList.Add(item1.ToString & item2.ToString)
            Next
        Next
        Return resultList
    End Function
Ok, So I need first insert all the listboxes during creation into the array?

Kind of  ListBoxes(x).Add(lstList)  something like that ?
Not exactly.
First set the array size:
ReDim ListBoxes(myRow("segmentCount"))
and after, in the for loop, assign the newly created listbox to each array item:
ListBoxes(x) = lstList
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dude, this is sick job thank youm very much, thk for video to show your exemple.
Hey Idle, if I dont want any seperator not even ""  what would I write at that line :

Permutations.Add(String.Join("", Iteration.ToArray))
What you just posted would do it.

"" is a blank string so it will Join them together with a blank string in-between:

    Permutations.Add(String.Join("", Iteration.ToArray))

It worked fine for me...squashing everything together.
Thanks.