Public Class Form1
    Inherits System.Windows.Forms.Form

#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

    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.IContainer

    '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.
  Friend WithEvents btnStart As System.Windows.Forms.Button
  Friend WithEvents btnExit As System.Windows.Forms.Button
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents txtNumber As System.Windows.Forms.TextBox
  Friend WithEvents txtTossCounter As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.btnStart = New System.Windows.Forms.Button()
    Me.btnExit = New System.Windows.Forms.Button()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.txtNumber = New System.Windows.Forms.TextBox()
    Me.txtTossCounter = New System.Windows.Forms.TextBox()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.SuspendLayout()
    '
    'btnStart
    '
    Me.btnStart.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.btnStart.Location = New System.Drawing.Point(8, 96)
    Me.btnStart.Name = "btnStart"
    Me.btnStart.Size = New System.Drawing.Size(104, 24)
    Me.btnStart.TabIndex = 0
    Me.btnStart.Text = "&Start"
    '
    'btnExit
    '
    Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.btnExit.Location = New System.Drawing.Point(128, 96)
    Me.btnExit.Name = "btnExit"
    Me.btnExit.Size = New System.Drawing.Size(104, 24)
    Me.btnExit.TabIndex = 1
    Me.btnExit.Text = "E&xit"
    '
    'Label1
    '
    Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label1.Location = New System.Drawing.Point(8, 8)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(120, 20)
    Me.Label1.TabIndex = 2
    Me.Label1.Text = "Heads in a row: "
    Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'txtNumber
    '
    Me.txtNumber.Location = New System.Drawing.Point(128, 8)
    Me.txtNumber.Name = "txtNumber"
    Me.txtNumber.Size = New System.Drawing.Size(104, 20)
    Me.txtNumber.TabIndex = 3
    Me.txtNumber.Text = "txtNumber"
    '
    'txtTossCounter
    '
    Me.txtTossCounter.Location = New System.Drawing.Point(128, 40)
    Me.txtTossCounter.Name = "txtTossCounter"
    Me.txtTossCounter.Size = New System.Drawing.Size(104, 20)
    Me.txtTossCounter.TabIndex = 5
    Me.txtTossCounter.Text = "txtTossCounter"
    '
    'Label2
    '
    Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label2.Location = New System.Drawing.Point(8, 40)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(120, 20)
    Me.Label2.TabIndex = 4
    Me.Label2.Text = "Number of tosses: "
    Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(248, 133)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtTossCounter, Me.Label2, Me.txtNumber, Me.Label1, Me.btnExit, Me.btnStart})
    Me.Name = "Form1"
    Me.Text = "Simulate coin toss"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Randomize()

  End Sub
  Private Function TossACoin() As Integer
    ' Purpose:  To simulate tossing a coin. The function
    '           return 1 for a Head, and 0 for a tail.
    '
    ' Parameters
    '   
    ' Return value
    '   integer       1 for Head, 0 for tails
    '
    TossACoin = Int(2 * Rnd())
  End Function

  Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Dim Tosses, Heads, ThisToss, Target As Integer

    Heads = 0
    Tosses = 0

    If txtNumber.Text <> "" Then
      Target = CInt(txtNumber.Text)
    End If
    Do
      ThisToss = TossACoin() ' Function returns 1 for head
      If ThisToss = 1 Then
        Heads += 1  ' It was a Heads
      Else
        Heads = 0   ' Tails…start over
      End If
      Tosses += 1   ' The toss counter
    Loop Until Heads = Target
    txtTossCounter.Text = CStr(Tosses)

  End Sub

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