0

I've got 4 listboxes, one for each type (headgear, inner shirt, outer shirt, pants).

A listbox would have elements of a single type (e.g. cap,red and sun hat,green...)

I want to be able to generate all permutations of all the elements from the 4 listboxes, and save the results to a text file.

So the output, for example, would contain:

  • cap,red | t-shirt,brown | jumper,brown | trousers,blue
  • sun hat,blue | t-shirt,brown | jacket,orange | shorts,grey

I've tried looking at this post, turning the listboxes into arrays, then generating the permutations. However, I can't get it to save to a file, or to a listbox then to a file.

So how do I generate all permutations from the four listboxes?

1 Answers1

0

These two procedures will do what you want. Although, if this is for anything more than a school assignment, I would suggest rather than working with listboxes as your data source for the permutations, create a class for each object(hat,shirt,jumper and trousers). Each one can then have it's own properties such as stock code,description, colour, size, order number,supplier etc. Then, generate the a details string for each item you want from description,colour and size, and finally generate the permutations from the details strings of each item. Of course ultimately you would be better using a database, but that is further down the line of developing your program.

Private Function GeneratePermutations() As List(Of String)
    Dim permList As New List(Of String)
    For Each hat As String In ListBox1.Items
        For Each shirt As String In ListBox2.Items
            For Each jumper As String In ListBox3.Items
                For Each trousers As String In ListBox4.Items
                    permList.Add(hat & "," & shirt & "," & jumper & "," & trousers)
                Next
            Next
        Next
    Next
    Return permList
End Function

Private Sub SaveFile(permlist As List(Of String), filename As String)
    If File.Exists(filename) Then
        Dim result As DialogResult = MessageBox.Show("File Exists, Overwrite? Y/N", "File Exists", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            Exit Sub
        End If
    End If
    Try
        Using sr As New StreamWriter(filename)
            For Each line As String In permlist
                sr.WriteLine(line)
            Next
        End Using
    Catch ex As Exception
        MessageBox.Show("Exception:" & ex.Message & vbCrLf & "Inner Exception :" & ex.InnerException.Message)
    End Try
End Sub

Example Usage

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim permutationList As New List(Of String)
    For i As Integer = 1 To 5
        ListBox1.Items.Add("Hat" & i.ToString)
        ListBox2.Items.Add("Shirt" & i.ToString)
        ListBox3.Items.Add("Jumper" & i.ToString)
        ListBox4.Items.Add("Trousers" & i.ToString)
    Next
    permutationList = GeneratePermutations()
    SaveFile(permutationList, "K:\perms.txt")
End Sub

These lines ..

permutationList = GeneratePermutations()
SaveFile(permutationList, "K:\perms.txt")

could be shortened further to

SaveFile(GeneratePermutations, "K:\perms.txt")

and the following line would then be unnecessary

Dim permutationList As New List(Of String)
David Wilson
  • 4,369
  • 3
  • 18
  • 31