tags:

views:

125

answers:

3

adding value together.

so:

1 + 0 = 1

3 + 1 = 4

4 + 4 = 8

How do I get this, so that it starts off at 0, and i type in 1 into a txtbox, so it adds 1 to the total, next i type in 3, so it adds 3 to the total, which eqauls 4, now i type 4, and it adds 4 to the total, which means its at 8 now.

How do i write the code to keep adding to its self?

+10  A: 

That sounds like homework, so I'll put the answer on a level so I don't give you the answer, but rather point out a direction.

The core problem is that you will need to store the value, so that it is available when you want to add the next number to it. If you declare a local variable inside a method, it gets initialized each time the method runs. So instead you will need to hold the value somewhere outside of the method, such as in a class field. Then you can simply add the value from the text box to the field by

  1. Reading the string from the text box
  2. Converting the string to a numerical type (such as int)
  3. Add the number to the total stored in the class field
Fredrik Mörk
+1 for taking the extra effort of explaining and directing rather than the easier way of just writing out the code
InSane
Knowing how it works > knowing it works.
Seidr
dude i frikken love you, this is what i came up with after reading your awnser: [code]public partial class Form1 : Form { int sum = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int o = int.Parse(textBox1.Text); sum = sum + o; textBox2.Text = sum.ToString(); } }[/code]
Codie Vincent
@Codie: I'm not ready for a relationship, but thanks anyway ;) On a serious note: look into using int.TryParse instead of int.Parse, in order to handle non-numeric input in a more graceful manner.
Fredrik Mörk
+1  A: 

A text-box is two things: it's an interactive control on the screen into which a user can type and it's a piece of state: the text that they have entered.

As you want to be able to add what the user has typed in to the previous value, you will need to store the previous value, so you will need a variable in which to keep the running total. (A variable declared as a member of a class is called a field.)

You will also need to decide how you will know when the update the value in the text box. Should this be when the user hits Return or will you have a separate GUI button that they have to press? Either way, you will then need to add the current value of the text-box to the value in your field and then update the text in the text-box with the calculated sum. Don't forget to update the field with the new sum.

Paul Ruane
+1  A: 
  • You have to allow for the user to express: "I am done entering this number." In my example, hitting enter while focus is on the text-box will serve this purpose.
  • You need to maintain the running total somewhere. In my example, a field on the form is serving this purpose.
  • You need to interpret the text in the text-box as a number. Beware, user-input is not reliable. In my example, I disregard invalid input.
  • You need to display the total to the user. In my example, I use a message-box.

int _total = 0;

public Form1()
{
    InitializeComponent();
    textBox1.KeyPress += textBox1_KeyPress;
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter)
    {
        int currentVal;
        if(!int.TryParse(textBox1.Text, out currentVal))
             return;

        _total += currentVal;
        textBox1.Clear();
        MessageBox.Show(_total.ToString());
    }
}
Ani