Hi i am just new to VB.NET and our teacher told us to experiment on for loop in a ODD SUM Program you need to add all odd numbers from Input One to Input Two like that but i can't stop it from adding continuously for example i input the first number 1 and the second 10 if i click the button one time the output should be 25 but when i click again the button it becomes 50 its wrong i can't terminate the process help me

Dim oddOne As Integer
        Dim oddTwo As Integer


     oddTwo = Val(txtInputTwo.Text)

        If txtInputOne.Text = "" And txtInputTwo.Text = "" Then
            MessageBox.Show("No Input, Please Input in Both Textboxes!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        Else
            If txtInputOne.Text = "" Then
                MessageBox.Show("No Input, Please Input in TextBox 1", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Else
                If txtInputTwo.Text = "" Then
                    MessageBox.Show("No Input, Please Input in TextBox 2", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Else
                    If IsNumeric(txtInputOne.Text) Or IsNumeric(txtInputTwo.Text) Then
                        If Val(txtInputOne.Text) Then
                            If Val(txtInputTwo.Text) Then
                                If Val(txtInputOne.Text) > Val(txtInputTwo.Text) Then
                                    MessageBox.Show("Your First Number Must be Greater Than the Second Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                                Else
                                    For oddOne = Val(txtInputOne.Text) To oddTwo

                                        If oddOne Mod 2 = 1 Then
                                            If oddOne < oddTwo Then
                                                lblOutput.Text = Val(lblOutput.Text) + oddOne
                                                oddOne += 1
                                            End If
                                        End If


                                    Next
                                End If
                            Else
                                MessageBox.Show("Wrong Input in TextBox2, Must be a Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                            End If
                        Else
                            MessageBox.Show("Wrong Input in TextBox 1, Must be a Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                        End If
                    Else
                        MessageBox.Show("Both Input are Incorrect Must Enter A Number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                    End If
                End If
            End If
        End If

Dani AI

Generated

A few concise fixes explain the behavior and give a reliable replacement approach.

The doubling on a second click happens because the previous result is never cleared before summing again — is right that the display needs to be reset each run. There are additional bugs in the posted routine: the For loop variable is being changed inside the loop (that causes skipped numbers and unpredictable behavior), the input checks use legacy functions and a wrong logical test, and an error message/condition appears reversed.

A safer pattern: parse both inputs with Integer.TryParse, validate the range, use a local accumulator initialized to zero, iterate without mutating the loop counter, then write the final sum to the label. Example:

Dim startValue As Integer
Dim endValue As Integer

If Not Integer.TryParse(txtInputOne.Text, startValue) OrElse Not Integer.TryParse(txtInputTwo.Text, endValue) Then
    MessageBox.Show("Enter valid integers.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
End If

If startValue > endValue Then
    MessageBox.Show("First number must be <= second number.", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
End If

Dim total As Integer = 0
For i As Integer = startValue To endValue
    If (i And 1) = 1 Then
        total += i
    End If
Next

lblOutput.Text = total.ToString()

For performance, start at the first odd and use Step 2 to halve iterations. Avoid Val/IsNumeric for parsing, do not modify the For variable inside the loop, and ensure any error messages match the actual comparison logic. A quick test: inputs 1 and 10 should always yield 25 even after repeated clicks.

Next time start with

lblOutput.Text = 0

this will clear the result from the previous calculation.
If the first time you've run your sub you got the result correct, then this should be your only problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.