Option Explicit On 
Option Strict On

Public Class frmSum
  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 Label2 As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents txtTarget As System.Windows.Forms.TextBox
  Friend WithEvents lblNumber As System.Windows.Forms.Label
  Friend WithEvents lblSum As System.Windows.Forms.Label
  Friend WithEvents btnSum 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.Label1 = New System.Windows.Forms.Label()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.txtTarget = New System.Windows.Forms.TextBox()
    Me.lblNumber = New System.Windows.Forms.Label()
    Me.lblSum = New System.Windows.Forms.Label()
    Me.btnSum = New System.Windows.Forms.Button()
    Me.SuspendLayout()
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(16, 8)
    Me.Label1.Name = "Label1"
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Target Sum"
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(16, 40)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(120, 23)
    Me.Label2.TabIndex = 1
    Me.Label2.Text = "Number of Integers"
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(16, 72)
    Me.Label3.Name = "Label3"
    Me.Label3.TabIndex = 2
    Me.Label3.Text = "Actual Sum"
    '
    'txtTarget
    '
    Me.txtTarget.Location = New System.Drawing.Point(136, 8)
    Me.txtTarget.Name = "txtTarget"
    Me.txtTarget.TabIndex = 3
    Me.txtTarget.Text = ""
    Me.txtTarget.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
    '
    'lblNumber
    '
    Me.lblNumber.BackColor = System.Drawing.Color.White
    Me.lblNumber.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblNumber.Location = New System.Drawing.Point(136, 40)
    Me.lblNumber.Name = "lblNumber"
    Me.lblNumber.TabIndex = 4
    Me.lblNumber.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'lblSum
    '
    Me.lblSum.BackColor = System.Drawing.Color.White
    Me.lblSum.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblSum.Location = New System.Drawing.Point(136, 72)
    Me.lblSum.Name = "lblSum"
    Me.lblSum.TabIndex = 4
    Me.lblSum.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'btnSum
    '
    Me.btnSum.Location = New System.Drawing.Point(80, 112)
    Me.btnSum.Name = "btnSum"
    Me.btnSum.Size = New System.Drawing.Size(104, 23)
    Me.btnSum.TabIndex = 5
    Me.btnSum.Text = "&Sum Integers"
    '
    'frmSum
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(258, 159)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSum, Me.lblSum, Me.lblNumber, Me.txtTarget, Me.Label3, Me.Label2, Me.Label1})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.Name = "frmSum"
    Me.Text = "Odd Integers Problem"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub btnSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSum.Click
    Dim NumberIntegers As Integer
    Dim CurrentInteger As Integer
    Dim TargetSum As Integer
    Dim Sum As Integer
    TargetSum = CInt(Val(txtTarget.Text))
    NumberIntegers = 1
    CurrentInteger = 1
    Sum = 1
    Do Until Sum >= TargetSum
      'get next integer and add to sum
      NumberIntegers += 1
      CurrentInteger += 2
      Sum += CurrentInteger
    Loop
    lblNumber.Text = Format(NumberIntegers, "#")
    lblSum.Text = Format(Sum, "#,###")
  End Sub
End Class
