Question using a text box to gather input from user into an array.


New member
Oct 21, 2013
Programming Experience
Im trying to make a small window forms app that will:

1. when the user inputs a decimal value in the subtotal textbox and the person hits the calculate button- it will display in the total textbox
2. when the user inputs a new value in the subtotal textbox it adds it to the previous total and displays the new total in its total textbox ... and so forth.
3. the values must then go into an array named 'totals' that has a cap of 10 values.
4. when the user hits the exit button, a message box should appear with all amounts entered- any totals entered as 0 should not display and then the program should exit- so
if the user inputs 10, 0, 35, 90, 45, 0, 10 then the message box should display 10 (newline) 35 (newline) 90 (newline) 45 (newline) 10.
5. if there is more than 10 values entered(including 0 values) it should throw an exception saying that there is an overflowexception- then display all previous entered
values (except 0 values) in a the message box- then the program should exit.

right now im stuck on how the values are inputted into the totals array.
how do I do that please?

the controls are as follows:

lblSubtotal = label
txtSubtotal = text box
lblTotal = label
txtTotal = text box (disabled so the user cant change the value in it)
btnCalculate = button
btnExit = button

I have some blocked out code... and some code to display the values - but all it throws out is the last entered values 10 times- probably cause I'm doing something wrong ;)
So far its been frustrating. Thanks again. :)
Sorry I'm still a newbie lol

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ArrayTotal
    public partial class frmTotals : Form
        public frmTotals()
            decimal subtotal = 0m;
            decimal total = 0m;
            const int MaxCount = 10;
            decimal[] totals = new decimal[MaxCount];

        private void btnCalculate_Click(object sender, EventArgs e)
            subtotal = Decimal.Parse(txtSubtotal.Text);
            total += subtotal;
            txtTotal.Text = total.ToString("c");
            for (int i = 0; i < totals.Length; i++)
            totals[i] = subtotal;

            for (int i = 0; i < totals.Length; i++)
            txtTotal.Text = subtotal.ToString();          

        private void btnExit_Click(object sender, EventArgs e)
            string totalsString = "";
            for (int i = 0; i < totals.Length; ++i)
                totalsString += totals[i] + "\n";
            MessageBox.Show(totalsString, "Sales Totals");
Last edited:
First of all, the code is messy and therefore hard to read. Please post code formatted properly inside [xcode=c#]your code here[/xcode] tags.

As for the question, what's the question? At what point does what actually happens differ from what you want to happen? Are there error messages? If so, what are they and where do they occur? If not, what's the aberrant behaviour? Please provide a FULL and CLEAR description so that we know what we're looking for.
I wanted to help in the first place but I'm not inclined to spend my time trying to work out what the problem is before even being able to work out a solution because a poster is stingy with the information they provide and how they provide it. I'd have provided help in the first place if the first post had been clear and I'd be providing help now if not for the unnecessary criticism.
Calm down dude or chick... I did fix the problem... and obviously the shoe fit- because it wasn't aimed at you.... take a chill pill and don't be too uptight. -I did say anyone not jmcilhinney
Anyhow anyone can help me or not- your choice- im just learning to program in C# that's all and needed a little help.

Coder Elitist not need apply! I guess some of us were born sucking on a keyboard from birth! :devilish: - and if the shoe fits for anyone one... suck it - the keyboard I mean... no- not directly intended at jmcilhinney
Top Bottom