Running total

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishercraigj
    New Member
    • Jan 2010
    • 16

    Running total

    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.ToDecim al(txtPointsEar ned.Text);
    decimal decTotalPointsE arned;

    //run code
    decTotalPointsE arned = decPointsEarned + decPointsEarned ;

    //display code
    lblTotalPointsE arned = decTotalPointsE arned.ToString( );
  • fishercraigj
    New Member
    • Jan 2010
    • 16

    #2
    PS - Yes, I'm a rookie. :)

    Comment

    • fishercraigj
      New Member
      • Jan 2010
      • 16

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

      Freaking typos...

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Better coding practice

        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.

        Code:
        decimal _runningtotal;
        decimal RunningTotal
        {
           get
           {
              return _runningtotal;
           }
           set
           {
              _runningtotal = value;
              if (tbTotalTextbox != null)
              {
                 tbTotalTextbox.Text = _runningtotal.ToString();
              }
           }
        }
        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

        Code:
        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.

        Comment

        • fishercraigj
          New Member
          • Jan 2010
          • 16

          #5
          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.

          Comment

          • fishercraigj
            New Member
            • Jan 2010
            • 16

            #6
            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.numericUpD own1.Controls[0].Hide();
            OR
            this.numericUpD own1.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.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              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.

              Comment

              Working...