これらの2つの手順は、必要な操作を行います。ただし、これが学校の授業以上のものであれば、リストボックスを並べ替えのデータソースとして使用する代わりに、各オブジェクト(帽子、シャツ、ジャンパー、ズボン)のクラスを作成することをお勧めします。それぞれは、株式コード、説明、色、サイズ、注文番号、サプライヤなどの独自のプロパティを持つことができます。次に、説明、色、サイズから必要な各項目の詳細文字列を生成し、最終的に各項目の詳細文字列。もちろん、最終的にはデータベースを使うほうが良いでしょうが、それはプログラム開発の一歩です。
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
使用例
permutationList = GeneratePermutations()
SaveFile(permutationList, "K:\perms.txt")
が
SaveFile(GeneratePermutations, "K:\perms.txt")
をさらに短縮することができ
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
これらの行..、次のLi neは不要となる。
Dim permutationList As New List(Of String)