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)