Philippe Renaud
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 !!!!
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 !!!!
Oops, did not read far enough :)
Let me get back to you .
Let me get back to you .
ASKER
hahahah :)
ASKER
By the way, i need the items that are Selected only. not all
You can use
Me.Forms.Controls.OfType(L istBox) 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.
Me.Forms.Controls.OfType(L
ASKER
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?
ASKER
I do :
For x As Integer = 0 To myRow("segmentCount")
Dim lstList As New ListBox
lstList.SelectionMode = SelectionMode.MultiExtende d
lstList.Name = "lstSegment" + CStr(x)
lstList.Location = New Point(myX, 200)
lstList.Size = New Size(200, 200)
etc....
Next
For x As Integer = 0 To myRow("segmentCount")
Dim lstList As New ListBox
lstList.SelectionMode = SelectionMode.MultiExtende
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.ToStr ing & item2.ToString)
Next
Next
Return resultList
End Function
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.ToStr
Next
Next
Return resultList
End Function
ASKER
Ok, So I need first insert all the listboxes during creation into the array?
Kind of ListBoxes(x).Add(lstList) something like that ?
Kind of ListBoxes(x).Add(lstList) something like that ?
Not exactly.
First set the array size:
ReDim ListBoxes(myRow("segmentCo unt"))
and after, in the for loop, assign the newly created listbox to each array item:
ListBoxes(x) = lstList
First set the array size:
ReDim ListBoxes(myRow("segmentCo
and after, in the for loop, assign the newly created listbox to each array item:
ListBoxes(x) = lstList
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dude, this is sick job thank youm very much, thk for video to show your exemple.
ASKER
Hey Idle, if I dont want any seperator not even "" what would I write at that line :
Permutations.Add(String.Jo in("", Iteration.ToArray))
Permutations.Add(String.Jo
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.Jo in("", Iteration.ToArray))
It worked fine for me...squashing everything together.
"" is a blank string so it will Join them together with a blank string in-between:
Permutations.Add(String.Jo
It worked fine for me...squashing everything together.
ASKER
Thanks.
Open in new window