Option Explicit On 
Option Strict On

Public Class frmTray
  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 txtLength As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents txtWidth As System.Windows.Forms.TextBox
  Friend WithEvents btnCompute As System.Windows.Forms.Button
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents lblCutSize As System.Windows.Forms.Label
  Friend WithEvents lblVolume As System.Windows.Forms.Label

  '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.txtWidth = New System.Windows.Forms.TextBox()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.txtLength = New System.Windows.Forms.TextBox()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.lblCutSize = New System.Windows.Forms.Label()
    Me.btnCompute = New System.Windows.Forms.Button()
    Me.lblVolume = New System.Windows.Forms.Label()
    Me.SuspendLayout()
    '
    'txtWidth
    '
    Me.txtWidth.Location = New System.Drawing.Point(120, 56)
    Me.txtWidth.Name = "txtWidth"
    Me.txtWidth.TabIndex = 1
    Me.txtWidth.Text = ""
    '
    'Label4
    '
    Me.Label4.Location = New System.Drawing.Point(16, 176)
    Me.Label4.Name = "Label4"
    Me.Label4.TabIndex = 0
    Me.Label4.Text = "Volume"
    Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'txtLength
    '
    Me.txtLength.Location = New System.Drawing.Point(120, 16)
    Me.txtLength.Name = "txtLength"
    Me.txtLength.TabIndex = 0
    Me.txtLength.Text = ""
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(16, 16)
    Me.Label1.Name = "Label1"
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Length (L)"
    Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(16, 56)
    Me.Label2.Name = "Label2"
    Me.Label2.TabIndex = 0
    Me.Label2.Text = "Width (W)"
    Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(16, 136)
    Me.Label3.Name = "Label3"
    Me.Label3.TabIndex = 0
    Me.Label3.Text = "Cut Size (X)"
    Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'lblCutSize
    '
    Me.lblCutSize.BackColor = System.Drawing.Color.White
    Me.lblCutSize.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblCutSize.Location = New System.Drawing.Point(120, 136)
    Me.lblCutSize.Name = "lblCutSize"
    Me.lblCutSize.TabIndex = 0
    Me.lblCutSize.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'btnCompute
    '
    Me.btnCompute.Location = New System.Drawing.Point(56, 96)
    Me.btnCompute.Name = "btnCompute"
    Me.btnCompute.Size = New System.Drawing.Size(128, 23)
    Me.btnCompute.TabIndex = 2
    Me.btnCompute.Text = "&Compute Cut Size"
    '
    'lblVolume
    '
    Me.lblVolume.BackColor = System.Drawing.Color.White
    Me.lblVolume.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.lblVolume.Location = New System.Drawing.Point(120, 176)
    Me.lblVolume.Name = "lblVolume"
    Me.lblVolume.TabIndex = 0
    Me.lblVolume.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    '
    'frmTray
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(238, 219)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblVolume, Me.lblCutSize, Me.Label3, Me.Label4, Me.btnCompute, Me.Label2, Me.txtWidth, Me.txtLength, Me.Label1})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.Name = "frmTray"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Tray Problem"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
    Dim L As Single
    Dim W As Single
    Dim X As Single, XMax As Single
    Dim Volume As Single, VolMax As Single
    Dim XBest As Single
    'Read L and W
    L = CSng(Val(txtLength.Text))
    W = CSng(Val(txtWidth.Text))
    If L <= 0 Or W <= 0 Then
      MessageBox.Show("L and W must both be positive", "Invalid values", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
      Exit Sub
    End If
    'Largest cut is one-half of smallest side
    If L > W Then
      XMax = W / 2
    Else
      XMax = L / 2
    End If
    'Go through 1000 possible cuts from 0 to XMax
    'Find largest volume
    VolMax = 0
    For X = 0 To XMax Step XMax / 1000
      Volume = X * (L - 2 * X) * (W - 2 * X)
      If Volume > VolMax Then
        XBest = X
        VolMax = Volume
      End If
    Next X
    'Display best values
    lblCutSize.Text = Format(XBest, "0.000")
    lblVolume.Text = Format(VolMax, "0.000")
  End Sub
End Class
