Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a textbox on a form in which numbers can be entered.

Once the enter key is pressed the number is entered.

There is no limit to however many numbers can be added, as I am using a counter.

I am unsure how to add these numbers using a For Next Loop, which then the answer is displayed in a message box.

I'm fairly new to Visual Basic and do not know how to do this in code and would really appreciate any help.


What I have so far:

VB
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        If Total.Text = "" Then
            MsgBox("Enter a valid number")
        Else
           	Counter+= 1
		Sum = 0 

For i = 1 To Counter
                Total =
                lblTotal.Text = Sum 
            Next i

            If Counter = 30 Then
                MsgBox("Maximum Data entered, Reset Data then enter additional data.")
            End If
        End If

    End Sub
Posted
Updated 3-Apr-17 2:46am
v7

FIRST ANSWER
What have you tried?
Where are you stuck?

Have you divided the problem in parts?

A) do you know how to prepare an event handler for your edit box? you will need to generate an event handler to catch the return key on that edit box and then react to the content of it.

B) have you limited the possibility to enter only numbers into that edit box? see this link[^].

C) do you know how to create a list that have any add method to capture the numbers and allow it to grow? (see this link[^]).

D) do you know how to create a message box? See this link[^].

E) about the for loop... well, see this link[^].

All your questions can be solved by trying to understand the problem as small parts and then implement them little by little. Don't try to make everything at once.

You should definitely explain a little bit more the last part about the for loop... and showing the result in a message box...

I can't understand why you need a loop to enter the values... the GUI will handle as many returns you press and each time you press return and a good entry has been processed you should be able to increase the counter inside the same enter key handler.

Then my question is: which result do you want to show? and when has this result to be applied?
If after each enter key press you want to show a message box then you don't need any loop here.

SECOND ANSWER
To convert a string to an integer you can use this:
VB
Dim str1 As String = Nothing
Dim int1 As Integer = Nothing

str1 = "10"

int1 = Convert.ToInt32(str1)


THIRD ANSWER
Given your code you've lied to everyone several times... :sigh:

See that you said in your question that: "Once the enter key is pressed" keypress event, now you say that you do that on the event of a button click.

You've not presented code and when you've done it the code is telling me different things than you question...

Well, I would do this:
VB
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  ' in order to make this work your textbox name has to replace "TextBoxValue"
  if (IsNumeric(TextBoxValue.Text))
  then
    lblTotal.Text = Convert.ToString(Convert.ToInt32(lblTotal.Text) + Convert.ToInt32(TextBoxValue.Text))
  end if
End Sub

Improve the way you make questions and you get a better result. It was impossible to answer what I've put here at the end before making all the steps I've made due to your bad question. See that I've spent time searching the Internet to find links (which you could have found), and that at the end and after a lot of comments I've given you a completely different solution.
I'm saying all this because here in CP you can find lot's of people willing to help, but this has been a clear example of a bad question.
Typically this kind of questions go directly to oblivion due to low votes.
Do it better the next time.

See the code. Try to understand it. If you have doubts then come back. ;)

PS: I've not compiled it, I've never worked with VB.NET, but this is a really easy thing to achieve.

Good luck.
 
Share this answer
 
v6
Comments
b2906 24-Aug-12 5:27am    
I am stuck on E, the loop. My problem is adding the value of what the user enters last onto the original sum, as I am not sure of how to word the addition in code. I have simplified the process by creating a label to display this. So far I have:

For i = 1 To ListCount
*I am not sure what goes here to add the values in the textbox*
lblTotal.Text = Sum
Next i
Joan M 24-Aug-12 5:32am    
You should have definitely posted a better question...

well, apart of that, you must declare a numeric variable, and then inside the loop put something like:

num_var = num_var + List[i]

it is important that the list contain numeric values... if it does not contain those values then you should force them to be numbers and not strings at the moment you insert them there.

At the end you can cast the number to a string and show it into the messagebox.
b2906 24-Aug-12 5:35am    
I have ensured the textbox can only contain numerical values.. but im still unsure of how i can add the inputed values to the global variable which is set to 0..
Joan M 24-Aug-12 5:37am    
let me ask you a question: do you need to keep all the values or you are only interested in the final sum?

I mean, if you don't need the list with all the values and you are interested in the sum only then you don't need the loop and neither the list itself.
Joan M 24-Aug-12 5:39am    
I've edited the solution to add a code snippet that converts strings to integers.
Take a global variable and intialize with zero. Then when a value is entering just add that value to that global value. Finally show that result in the MessageBox.
 
Share this answer
 

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