Public Class Form1
  Inherits System.Windows.Forms.Form
  Dim kidz As Kids
  Friend WithEvents cbClubs As System.Windows.Forms.ComboBox
  Friend WithEvents lscKids As System.Windows.Forms.ListBox
  Friend WithEvents lsKids As System.Windows.Forms.ListBox
  Dim clubs As ArrayList

  Private Sub init()
    Dim iter As Iterator
    Dim kd As Kid
    Dim i As Integer

    clubs = New ArrayList()
    kidz = New Kids("50free.txt")
    'Inicjuj  kolekcję
    'I wczytaj dane z pliku
    'Traktuj kolekcję jak iterator
    iter = kidz.getIterator
    While iter.hasMoreElements    'Załaduj na listę
      kd = CType(iter.nextElement, Kid)
      lsKids.Items.Add(kd.getFrname & " " & kd.getLname)
      addClub(kd.getClub)
    End While
    sortClubs()
    For i = 0 To clubs.Count() - 1
      cbClubs.Items.Add(clubs.Item(i))
    Next i
    cbClubs.SelectedIndex = 0
  End Sub

  Private Sub addClub(ByRef club As String)
    Dim i As Integer
    Dim found As Boolean
    found = False
    i = 0
    While i < clubs.Count And Not found
      found = (CType(clubs(i), String) = club)
      i = i + 1
    End While
    If Not found Then
      clubs.Add(club)
    End If
  End Sub

  Private Sub sortClubs()
    Dim cb(clubs.Count) As String
    Dim j, i, max As Integer
    Dim tmp As String

    max = clubs.Count()
    For i = 0 To max - 1
      cb(i) = CType(clubs(i), String)
    Next i
    For i = 0 To max - 1
      For j = i To max - 1
        If cb(i) > cb(j) Then
          tmp = cb(i)
          cb(i) = cb(j)
          cb(j) = tmp
        End If
      Next j
    Next i
    clubs = Nothing
    clubs = New ArrayList()
    For i = 0 To max - 1
      clubs.Add(cb(i))
    Next i
  End Sub

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    init()
  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.Container

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.lsKids = New System.Windows.Forms.ListBox()
    Me.lscKids = New System.Windows.Forms.ListBox()
    Me.cbClubs = New System.Windows.Forms.ComboBox()
    Me.SuspendLayout()
    '
    'lsKids
    '
    Me.lsKids.Location = New System.Drawing.Point(8, 16)
    Me.lsKids.Name = "lsKids"
    Me.lsKids.Size = New System.Drawing.Size(136, 173)
    Me.lsKids.TabIndex = 0
    '
    'lscKids
    '
    Me.lscKids.Location = New System.Drawing.Point(208, 48)
    Me.lscKids.Name = "lscKids"
    Me.lscKids.Size = New System.Drawing.Size(112, 134)
    Me.lscKids.TabIndex = 1
    '
    'cbClubs
    '
    Me.cbClubs.DropDownWidth = 104
    Me.cbClubs.Location = New System.Drawing.Point(216, 8)
    Me.cbClubs.Name = "cbClubs"
    Me.cbClubs.Size = New System.Drawing.Size(104, 21)
    Me.cbClubs.TabIndex = 2
    Me.cbClubs.Text = "ComboBox1"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(344, 237)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cbClubs, Me.lscKids, Me.lsKids})
    Me.Name = "Form1"
    Me.Text = "Kid Iterator"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbClubs.SelectedIndexChanged
    Dim club As String
    Dim iter As Iterator
    Dim kd As Kid

    club = cbClubs.Text
    iter = kidz.getClubIterator(club)
    lscKids.Items.Clear()
    While iter.hasMoreElements
      kd = CType(iter.nextElement, Kid)
      lscKids.Items.Add(kd.getFrname & " " & kd.getLname)
    End While

  End Sub
End Class
