I've created a form that determines the highest number out of 4 text boxes after adding a timer to self eval it broke

To: All

This is an odd one.

The program has 4 text boxes.  I'll start by entering 12, 11, 10, 9 and click evalute and it will show that 12 is the greatest number.

I can change any number and it will still work.

It dies when I empty/delete a field.  I have added a checker to see it a blank exist and ignore it but that doesn't seem to help.

Thanks inadvance


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

    Dim WithEvents tmrMyTimer As New Timers.Timer(2000)


    '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 Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents resultField As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents item1 As System.Windows.Forms.TextBox
    Friend WithEvents item2 As System.Windows.Forms.TextBox
    Friend WithEvents item3 As System.Windows.Forms.TextBox
    Friend WithEvents item4 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.item1 = New System.Windows.Forms.TextBox()
        Me.item2 = New System.Windows.Forms.TextBox()
        Me.item3 = New System.Windows.Forms.TextBox()
        Me.item4 = 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.Label4 = New System.Windows.Forms.Label()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.resultField = New System.Windows.Forms.TextBox()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'item1
        '
        Me.item1.Location = New System.Drawing.Point(88, 80)
        Me.item1.Name = "item1"
        Me.item1.Size = New System.Drawing.Size(64, 20)
        Me.item1.TabIndex = 0
        Me.item1.Text = ""
        '
        'item2
        '
        Me.item2.Location = New System.Drawing.Point(176, 80)
        Me.item2.Name = "item2"
        Me.item2.Size = New System.Drawing.Size(64, 20)
        Me.item2.TabIndex = 1
        Me.item2.Text = ""
        '
        'item3
        '
        Me.item3.Location = New System.Drawing.Point(272, 80)
        Me.item3.Name = "item3"
        Me.item3.Size = New System.Drawing.Size(64, 20)
        Me.item3.TabIndex = 2
        Me.item3.Text = ""
        '
        'item4
        '
        Me.item4.Location = New System.Drawing.Point(360, 80)
        Me.item4.Name = "item4"
        Me.item4.Size = New System.Drawing.Size(64, 20)
        Me.item4.TabIndex = 3
        Me.item4.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(88, 48)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 16)
        Me.Label1.TabIndex = 14
        Me.Label1.Text = "Item1"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(184, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 16)
        Me.Label2.TabIndex = 15
        Me.Label2.Text = "Item2"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(272, 48)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(64, 16)
        Me.Label3.TabIndex = 16
        Me.Label3.Text = "Item3"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(360, 48)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(64, 16)
        Me.Label4.TabIndex = 17
        Me.Label4.Text = "Item4"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 176)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(144, 24)
        Me.Button1.TabIndex = 23
        Me.Button1.Text = "Evaluate"
        '
        'resultField
        '
        Me.resultField.Location = New System.Drawing.Point(32, 224)
        Me.resultField.Name = "resultField"
        Me.resultField.Size = New System.Drawing.Size(680, 20)
        Me.resultField.TabIndex = 24
        Me.resultField.Text = ""
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(232, 176)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(120, 24)
        Me.Button2.TabIndex = 25
        Me.Button2.Text = "Stop Eval"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(736, 389)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.resultField, Me.Button1, Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.item4, Me.item3, Me.item2, Me.item1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Private Function GetHand(ByVal i1 As String, ByVal i2 As String, ByVal i3 As String, ByVal i4 As String) As String

        Dim results As String

        Dim maxValue As Integer

        Dim counter As New Hashtable()

       

        If (i1.Length.ToString <> 0) Then
            counter(i1) += 1
        End If

        If (i2.Length.ToString <> 0) Then
            counter(i2) += 1
        End If

        If (i3.Length.ToString <> 0) Then
            counter(i3) += 1
        End If

        If (i4.Length.ToString <> 0) Then
            counter(i4) += 1
        End If




        Dim en As IDictionaryEnumerator = counter.GetEnumerator

        While en.MoveNext

            ' MessageBox.Show("Inside While")

            results = results & en.Key & "=>" & en.Value & " " 'Console.WriteLine(en.Key & " : " & en.Value)

            ' Capture Value

            If (en.Key > maxValue) Then

                maxValue = en.Key

            End If


        End While

        results = results & " maxValue => " & maxValue

        'MessageBox.Show("Returning Result")

        Return results



    End Function

   

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        tmrMyTimer.Start()
    End Sub


    Sub tmrEvent(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs) Handles tmrMyTimer.Elapsed
        tmrMyTimer.Stop()
        'timer has elapsed
        'MessageBox.Show("help")
        resultField.Text = GetHand(item1.Text, item2.Text, item3.Text, item4.Text)
        tmrMyTimer.Start()
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        tmrMyTimer.Stop()
    End Sub
End Class
LVL 1
vaultworldAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
This code bothers me:

        If (i1.Length.ToString <> 0) Then
            counter(i1) += 1
        End If

        If (i2.Length.ToString <> 0) Then
            counter(i2) += 1
        End If

        If (i3.Length.ToString <> 0) Then
            counter(i3) += 1
        End If

        If (i4.Length.ToString <> 0) Then
            counter(i4) += 1
        End If

Why are you converting Length to a String and then comparing it with a number?  Either change 0 to "0" or don't use the ToString part:

        If (i1.Length <> 0) Then
            counter(i1) += 1
        End If

        ' etc..
0
 
S-TwilleyCommented:
The purpose of GetHand() is to return the highest of the 4 values?
0
 
S-TwilleyCommented:
maybe they meant to do...

        If (i1.ToString.Length <> 0) Then
            counter(i1) += 1
        End If

==========

although that is kinda useless since i1 is a string already

=========

I'm just waitin to see what that function does... my tired head can't compute what it's meant to be doing, so I just asked and was planning on writing something from scratch instead....  but I might just leave it in your capable hands :P
0
 
vaultworldAuthor Commented:
thanks, I swear I tried that first and it didn't work but apparently not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.