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

Posted on 2005-04-13
Last Modified: 2010-04-23
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()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.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 = ""
        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 = ""
        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 = ""
        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 = ""
        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"
        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"
        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"
        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"
        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"
        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 = ""
        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"
        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"

    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
    End Sub

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

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    End Sub
End Class
Question by:vaultworld
    LVL 12

    Expert Comment

    The purpose of GetHand() is to return the highest of the 4 values?
    LVL 85

    Accepted Solution

    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..
    LVL 12

    Expert Comment

    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
    LVL 1

    Author Comment

    thanks, I swear I tried that first and it didn't work but apparently not.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now