What is the code for adding numerical values in textboxes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gio1405
    New Member
    • Mar 2010
    • 1

    What is the code for adding numerical values in textboxes?

    I am kind of a beginner in C# and I have to do a program in which i have to add all the numerical values of the text boxes. My code starts like this:

    float.nTxtbox1 = float.parse (txtbox1.text);

    float.nTxtbox2 = float.parse (txtbox2.text);

    float.nTxtbox3 = float.parse (txtbox3.text);

    float.nTxtbox4 = float.parse (txtbox4.text);

    float.nTxtbox5 = float.parse (txtbox5.text);

    float.nTotal = float.parse (txtTotal.text) ;

    nTotal = (nTxtbox1+nTxtb ox2+nTxtbox3+nT xtbox4+nTxtbox5 )

    But it seems that this kind of code doesn't work.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    What do you mean by does not work?
    So you want the total need to be written back in the text box.?

    I can see a text box with name Total and i assume you want to write the total value back to this text box. In this case you need to write the value in the text box.

    You have calculated the total but you never written it back to text box.

    Regards
    Dheeraj Joshi

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      Hi,

      What did you mean by "float.nTxtbox1 "?

      Plus the TextBox has the property "Text" not "text".
      Finally after you calculate your data you have to assign that result to the "Text" property of your total textBox, which I don't see!


      Thanks,
      Bassem

      Comment

      • Samishii23
        New Member
        • Sep 2009
        • 246

        #4
        Google

        You can find an answer easier this way. The 1st search result was about a money input.

        Comment

        • queried
          New Member
          • Mar 2010
          • 4

          #5
          Using Regex.Replace() could be an easy one workaround. Just add System.Text.Reg ularExpressions reference to your `using` section, then create and assign EventHandler for TextChanged events of your textboxes. There you can replace all symbols except digits to "". Pattern in such case would be pretty simple:

          "[^0-9]"

          Comment

          Working...