Public Class frmRandomNumbers
  Inherits System.Windows.Forms.Form
  Dim Elements, Numbers() As Integer


  Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnGen.Click
    Dim i As Integer

    ReDim Numbers(Elements)
    ' Pamiętajmy, jeśli Elements = 10, otrzymujemy faktycznie 
    ' 11 elementów (0 – 10)
    txtNumList.Text = ""
    For i = 0 To Elements
      Numbers(i) = CInt(Rnd() * 100)
      txtNumList.Text &= CStr(Numbers(i)) & vbCrLf
    Next i
    txtSearch.Visible = False
    Label2.Visible = False
    btnFind.Visible = False

  End Sub

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnExit.Click
    Me.Dispose()
  End Sub

  Private Sub txtElements_Leave(ByVal sender As Object, ByVal e As _
                 System.EventArgs) Handles txtElements.Leave
    Elements = CInt(txtElements.Text)
  End Sub

  Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnSort.Click
    Dim i As Integer

    Array.Sort(Numbers)
    txtNumList.Text = ""
    For i = 0 To Elements
      txtNumList.Text &= Format(i, "00") & " " & CStr(Numbers(i)) & vbCrLf
    Next
    txtSearch.Visible = True
    Label2.Visible = True
    btnFind.Visible = True

  End Sub

  Private Sub frmRandomNumbers_Load(ByVal sender As System.Object, _
                 ByVal e As System.EventArgs) Handles MyBase.Load
    txtSearch.Visible = False
    Label2.Visible = False
    btnFind.Visible = False

  End Sub

  Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As _
                 System.EventArgs) Handles btnFind.Click
    Dim val As Integer

    val = CInt(txtSearch.Text)
    txtSearch.Text &= " = " & CStr(Array.BinarySearch(Numbers, val))

  End Sub

End Class
