Connect with Expertise | Find Experts, Get Answers, Share Insights

Running total

 
Join Date: Jan 2010
Posts: 15
#1: Feb 7 '10
How do I code variables for a simple "running total" box without using an array?

IE: I have a "Points Earned" text box that the user inputs a value into. I have another output "Total Points Earned" box that keeps a running count of all points earned every time the user inputs a new value.

I've been doing this (which is obviously wrong):

//declare variables
decimal decPointsEarned = Convert.ToDecimal(txtPointsEarned.Text);
decimal decTotalPointsEarned;

//run code
decTotalPointsEarned = decPointsEarned + decPointsEarned;

//display code
lblTotalPointsEarned = decTotalPointsEarned.ToString();

 
Join Date: Jan 2010
Posts: 15
#2: Feb 7 '10

re: Running total


PS - Yes, I'm a rookie. :)
 
Join Date: Jan 2010
Posts: 15
#3: Feb 7 '10

re: Running total


ah, figured out what was going on. Never mind!

Freaking typos...
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#4: Feb 7 '10

re: Running total


I might suggest you start practicing some better coding practices. The sooner you do it right, the less difficult it is to unlearn bad habits.

Like don't use the textbox as a place to store information. That's what variable and properties are for. Textboxes are pretty GUI things for displaying information.

Make a decimal property with get and set methods. When someone sets a value to the decimal property, it's set method can then update the textbox.

Expand|Select|Wrap|Line Numbers
  1. decimal _runningtotal;
  2. decimal RunningTotal
  3. {
  4.    get
  5.    {
  6.       return _runningtotal;
  7.    }
  8.    set
  9.    {
  10.       _runningtotal = value;
  11.       if (tbTotalTextbox != null)
  12.       {
  13.          tbTotalTextbox.Text = _runningtotal.ToString();
  14.       }
  15.    }
  16. }
Now anywhere else in your code you have a decimal variable you can work with, instead of constantly converting your Textbox.Text for every calculation

Expand|Select|Wrap|Line Numbers
  1. RunningTotal += 25;
Second. Don't use textboxes for strict numeric input since you then after keep validating the user input isn't "cat" instead of '4'. Use a NumericUpDown. The user can still type a number, and you can turn off the up/down spin controls in the properties of the of control so it looks like a textbox, but only accepts numbers and you can set minimum and maximum acceptable values.
 
Join Date: Jan 2010
Posts: 15
#5: Feb 8 '10

re: Running total


Whoa, that makes total sense. I was having troubles with user typing characters instead of numerics. I'm not sure why my c# teacher taught us the way that I showed you?! Maybe you should come teach the class!

Thanks again, dude. If you you were local Seattle, I'd totally buy you a beer right now for all the good advice. Trust that it doesn't go un-noticed.
 
Join Date: Jan 2010
Posts: 15
#6: Feb 8 '10

re: Running total


You sure you can turn off the spin controls for a NUD? I've been looking for about an hour for a solution online to turn it off. The best I came up with is:

this.numericUpDown1.Controls[0].Hide();
OR
this.numericUpDown1.Controls[0].Visible = false;

It does in fact remove the UpDown Arrows but it leaves an indented gray background when you run it half the time and the other half of the time it's a indented white background. Totally looks weird. Sometimes it adds pixels or characters where the arrow keys would have been displayed. Seems kind of like a bug in VS.

How do you hide your spin keys? Sorry to ask all these questions as of late.
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#7: Feb 8 '10

re: Running total


You sure you can turn off the spin controls for a NUD? I've been looking for about an hour for a solution online to turn it off.
My mistake. Its the DateTimePicker that has a property of "ShowUpDown" true/false.
Reply