Option Explicit On 
Option Strict On

Public Class frmStats
  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
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents lblNumber As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents txtInput As System.Windows.Forms.TextBox
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents lblMean As System.Windows.Forms.Label
  Friend WithEvents lblStdDev As System.Windows.Forms.Label
  Friend WithEvents btnExit As System.Windows.Forms.Button
  Friend WithEvents btnNew As System.Windows.Forms.Button
  Friend WithEvents btnAccept As System.Windows.Forms.Button
  Friend WithEvents btnCompute As System.Windows.Forms.Button

  '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.btnNew = New System.Windows.Forms.Button()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.Label5 = New System.Windows.Forms.Label()
    Me.lblStdDev = New System.Windows.Forms.Label()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.btnAccept = New System.Windows.Forms.Button()
    Me.lblMean = New System.Windows.Forms.Label()
    Me.btnExit = New System.Windows.Forms.Button()
    Me.btnCompute = New System.Windows.Forms.Button()
    Me.lblNumber = New System.Windows.Forms.Label()
    Me.txtInput = New System.Windows.Forms.TextBox()
    Me.SuspendLayout()
    '
    'btnNew
    '
    Me.btnNew.Location = New System.Drawing.Point(8, 112)
    Me.btnNew.Name = "btnNew"
    Me.btnNew.Size = New System.Drawing.Size(104, 23)
    Me.btnNew.TabIndex = 6
    Me.btnNew.Text = "&New Sequence"
    '
    'Label4
    '
    Me.Label4.Location = New System.Drawing.Point(16, 152)
    Me.Label4.Name = "Label4"
    Me.Label4.TabIndex = 0
    Me.Label4.Text = "Mean"
    '
    'Label5
    '
    Me.Label5.Location = New System.Drawing.Point(16, 184)
    Me.Label5.Name = "Label5"
    Me.Label5.Size = New System.Drawing.Size(112, 23)
    Me.Label5.TabIndex = 2
    Me.Label5.Text = "Standard Deviation"
    '
    'lblStdDev
    '
    Me.lblStdDev.BackColor = System.Drawing.Color.White
    Me.lblStdDev.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblStdDev.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.lblStdDev.Location = New System.Drawing.Point(144, 184)
    Me.lblStdDev.Name = "lblStdDev"
    Me.lblStdDev.TabIndex = 1
    Me.lblStdDev.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 8)
    Me.Label1.Name = "Label1"
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Number of Values"
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(8, 48)
    Me.Label3.Name = "Label3"
    Me.Label3.TabIndex = 2
    Me.Label3.Text = "Enter Number"
    '
    'btnAccept
    '
    Me.btnAccept.Location = New System.Drawing.Point(8, 80)
    Me.btnAccept.Name = "btnAccept"
    Me.btnAccept.Size = New System.Drawing.Size(104, 23)
    Me.btnAccept.TabIndex = 4
    Me.btnAccept.Text = "&Accept Number"
    '
    'lblMean
    '
    Me.lblMean.BackColor = System.Drawing.Color.White
    Me.lblMean.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblMean.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.lblMean.Location = New System.Drawing.Point(144, 152)
    Me.lblMean.Name = "lblMean"
    Me.lblMean.TabIndex = 1
    Me.lblMean.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'btnExit
    '
    Me.btnExit.Location = New System.Drawing.Point(144, 112)
    Me.btnExit.Name = "btnExit"
    Me.btnExit.Size = New System.Drawing.Size(104, 23)
    Me.btnExit.TabIndex = 7
    Me.btnExit.Text = "E&xit"
    '
    'btnCompute
    '
    Me.btnCompute.Location = New System.Drawing.Point(144, 80)
    Me.btnCompute.Name = "btnCompute"
    Me.btnCompute.Size = New System.Drawing.Size(104, 23)
    Me.btnCompute.TabIndex = 5
    Me.btnCompute.Text = "&Compute"
    '
    'lblNumber
    '
    Me.lblNumber.BackColor = System.Drawing.Color.White
    Me.lblNumber.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblNumber.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.lblNumber.Location = New System.Drawing.Point(144, 8)
    Me.lblNumber.Name = "lblNumber"
    Me.lblNumber.TabIndex = 1
    Me.lblNumber.Text = "0"
    Me.lblNumber.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'txtInput
    '
    Me.txtInput.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.txtInput.Location = New System.Drawing.Point(144, 40)
    Me.txtInput.Name = "txtInput"
    Me.txtInput.TabIndex = 3
    Me.txtInput.Text = ""
    '
    'frmStats
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(272, 221)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblStdDev, Me.lblMean, Me.Label4, Me.Label5, Me.btnExit, Me.btnNew, Me.btnCompute, Me.btnAccept, Me.txtInput, Me.Label3, Me.lblNumber, Me.Label1})
    Me.Name = "frmStats"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Mean and Standard Deviation"
    Me.ResumeLayout(False)

  End Sub

#End Region
  Dim NumValues As Integer
  Dim SumX As Double
  Dim SumX2 As Double

  Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
    Dim Value As Double
    'make sure there is something there
    If txtInput.Text = "" Then
      Exit Sub
    End If
    'Get number and sum number and number-squared
    NumValues += 1
    lblNumber.Text = Str(NumValues)
    Value = Val(txtInput.Text)
    SumX += Value
    SumX2 += Value ^ 2
    txtInput.Text = ""
    txtInput.Focus()
  End Sub

  Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
    Dim Mean As Double
    Dim StdDev As Double
    'Make sure there are at least two values
    If NumValues < 2 Then
      Beep()
      Exit Sub
    End If
    Mean = SumX / NumValues
    lblMean.Text = Str(Mean)
    'Compute standard deviation
    StdDev = Math.Sqrt((NumValues * SumX2 - SumX ^ 2) / (NumValues * (NumValues - 1)))
    lblStdDev.Text = Str(StdDev)
    txtInput.Focus()
  End Sub

  Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
    'Initialize all variables
    NumValues = 0
    lblNumber.Text = "0"
    txtInput.Text = ""
    lblMean.Text = ""
    lblStdDev.Text = ""
    SumX = 0
    SumX2 = 0
    txtInput.Focus()
  End Sub


  Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
    'Only allow numbers,backspace, minus sign, decimal point, return
    Select Case e.KeyChar
      Case CChar("0") To CChar("9"), ControlChars.Back
        'number or backspace
        e.Handled = False
      Case CChar("-")
        'make sure only one and in first position
        If InStr(txtInput.Text, "-") <> 0 Or txtInput.SelectionStart <> 0 Then
          e.Handled = True
        Else
          e.Handled = False
        End If
      Case CChar(".")
        'allow only one
        If InStr(txtInput.Text, ".") = 0 Then
          e.Handled = False
        Else
          e.Handled = True
        End If
      Case ControlChars.Cr
        btnAccept.PerformClick()
        e.Handled = False
      Case Else
        e.Handled = True
    End Select
  End Sub

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