Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to find the sum of 5 nos. entered by the user, with only 1 textbox and 1 button, everytime I input a no. in the textbox, I also click the button, then clear the textbox and repeat the process until I have input 5 nos, the 5th and last time I clicked the button, the msgbox will show the sum of 5 nos. entered by the user, and will not be able to input another no. because the textbox will not be visible because the limit is reached only until 5 times, what is the vb .net code for this problem? Using if-then conditional statements and/or while loop only.

What I have tried:

Dim s, i, n As Integer
s = 0
i = 0
n = TextBox1.Text

If i < 5 Then
i = i + 1
sum = sum + n
Msgbox("The sum is : " & sum)
End If

End Sub
End Class

But its not really working. Incomplete.
Posted
Comments
Ralf Meier 12-Sep-17 10:41am    
This could not work because this isn't a code which could work ...
All you have to code is to do what you have described ... The action must come from the Button.Click-Event.
Perhaps you try it with that ...

1 solution

Below is a working example based on the requirements outlined. Notice that the names that I give controls and variables make the code very easy to read.
VB
Public Class Form1

    Private Total As Integer
    Private Count As Integer
    Private MaxEntries As Integer = 5

    Private Sub ButAdd_Click(sender As Object, e As EventArgs) Handles ButAdd.Click

        If String.IsNullOrEmpty(txtInput.Text.Trim) Then Return

        Dim Value As Integer

        If Integer.TryParse(txtInput.Text, Value) Then

            If Count < MaxEntries Then
                Count += 1
                Total += Value
                labTotal.Text = Total.ToString
            Else
                txtInput.Enabled = False
            End If

        End If

        txtInput.ResetText()
        txtInput.Select()

    End Sub

End Class
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900