how to apply mathematic calculation on textbox.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinay2110
    New Member
    • Feb 2008
    • 6

    how to apply mathematic calculation on textbox.

    i m new to .net programing and dont know how to apply mathematic calculation on the text box. i has the idea to do this thing using auto post back but i dont know what is the exact syntax which will perform the required operation like add, subtract, %calculation.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by vinay2110
    i m new to .net programing and dont know how to apply mathematic calculation on the text box. i has the idea to do this thing using auto post back but i dont know what is the exact syntax which will perform the required operation like add, subtract, %calculation.
    So you have a TextBox that has some text in it.
    On the server, when you click a button, take this text and parse it into an Integer or Double (or whatever you want to use) and then perform your mathematical operation on this variable.

    eg:
    [code=vbnet]
    Protected Sub Btn_Add_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Btn_Add.Click

    Try
    Dim myNumber As Integer = Integer.Parse(t heNumberTextBox .Text)
    Dim result As Integer = myNumber + 20
    myResultLabel.T ext = result.ToString
    Catch ex As ArgumentNullExc eption
    'number supplied was nothing
    myResultLabel.T ext = "Please Supply a Number"
    Catch ex As FormatException
    'number supplied was not a number
    myResultLabel.T ext = "Please Supply a Number"
    Catch ex As OverflowExcepti on
    'number supplied was a number too large or too small to be an Integer
    myResultLabel.T ext = "Please Supply a Valid Number"

    End Try
    End Sub
    [/code]

    -Frinny

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Just for clarification, are you wanting to input a number in a TextBox, and then apply some calculation to it? Or do you want to put a mathematical statement into a TextBox and have the program solve it?

      Also, what language (VB/C#)?

      Originally posted by vinay2110
      i m new to .net programing and dont know how to apply mathematic calculation on the text box. i has the idea to do this thing using auto post back but i dont know what is the exact syntax which will perform the required operation like add, subtract, %calculation.

      Comment

      • vinay2110
        New Member
        • Feb 2008
        • 6

        #4
        Originally posted by insertAlias
        Just for clarification, are you wanting to input a number in a TextBox, and then apply some calculation to it? Or do you want to put a mathematical statement into a TextBox and have the program solve it?

        Also, what language (VB/C#)?



        i want to input number in that textboxes.the only thing i want that the result of these calculation will be displayed in other textboxes. i m using C# .please send me solution to this problem i will be greatly thankful to you.

        Comment

        • vinay2110
          New Member
          • Feb 2008
          • 6

          #5
          Originally posted by Frinavale
          So you have a TextBox that has some text in it.
          On the server, when you click a button, take this text and parse it into an Integer or Double (or whatever you want to use) and then perform your mathematical operation on this variable.

          eg:
          [code=vbnet]
          Protected Sub Btn_Add_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Btn_Add.Click

          Try
          Dim myNumber As Integer = Integer.Parse(t heNumberTextBox .Text)
          Dim result As Integer = myNumber + 20
          myResultLabel.T ext = result.ToString
          Catch ex As ArgumentNullExc eption
          'number supplied was nothing
          myResultLabel.T ext = "Please Supply a Number"
          Catch ex As FormatException
          'number supplied was not a number
          myResultLabel.T ext = "Please Supply a Number"
          Catch ex As OverflowExcepti on
          'number supplied was a number too large or too small to be an Integer
          myResultLabel.T ext = "Please Supply a Valid Number"

          End Try
          End Sub
          [/code]

          -Frinny


          thanks for sending the reply but i m using c# and i wants that i input the number s in the textboxes and the result will be shown in the another textbox . i m using auto post back property for this.please send me solution to this problem
          i will be greatly thankful to you.

          Comment

          • Svinja
            New Member
            • Nov 2007
            • 25

            #6
            Hi, this sample takes two number from textBox1 and textBox2, adds them, and outputs them to textBox3:
            Code:
            Int32 number1 = Convert.ToInt32(textBox1.Text);
            Int32 number2 = Convert.ToInt32(textBox2.Text);
            Int32 number3 = number1 + number2;
            textBox3.Text = number3.ToString();

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by vinay2110
              thanks for sending the reply but i m using c# and i wants that i input the number s in the textboxes and the result will be shown in the another textbox . i m using auto post back property for this.please send me solution to this problem
              i will be greatly thankful to you.

              In the future please specify what language you are using in advanced. Visual Basic and C# .NET work the same way, the only difference is the syntax. Therefore if I post VB.NET code you should be able to get the concepts and apply it in your C# code.

              The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

              I do not understand why you are using AutoPostback on your text boxes...normall y people have 2 TextBoxes and a button that represents the calculation that you want to perform on the TextBox's Text.

              Eg

              TextboxWithValu e1 TextboxWithValu e2

              ButtonForAdditi on ButtonForSubtra ction ButtonForDivisi on ButtonForModulo us

              TextboxWithResu lt



              Then when the user clicks the ButtonForAdditi on, in the Click event for this button you take the Text in the TextboxWithValu e1 and TextboxWithValu e1 and do the calculation.


              Since you are doing AutoPostback you will have to put this code in method that handles your TextChanged Event for the textboxes....

              eg

              [code=cpp]
              Private void TextboxWithValu e1_TextChanged( Object sender, EventArgs e) Handles TextboxWithValu e1.TextChanged
              {
              //Do your calculations here.
              //Remember, to Get and Set the text from the TextBoxes you use the Text Property
              //Eg: TextboxWithValu e1.Text will return the text in the text box
              //Remember that you do mathematical calculations on data types like Integer, double, float etc...not on Strings. Therefore you need to convert the text in the textboxes into these type. If you are using Integers, use Integer.Parse to convert the text, if you're using a Double use Double.Parse... I'm sure you get the idea from this....or you can use Convert as Svinja has suggested in the last post
              }

              Private void TextboxWithValu e2_TextChanged( Object sender, EventArgs e) Handles TextboxWithValu e2.TextChanged
              {
              //Do your calculations here.
              }
              [/code]

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Originally posted by vinay2110
                thanks for sending the reply but i m using c# and i wants that i input the number s in the textboxes and the result will be shown in the another textbox . i m using auto post back property for this.please send me solution to this problem
                i will be greatly thankful to you.
                Auto post back doesn't seem like a good idea. If you want the calculations to be applied in real time (meaning no page refresh) as the user enters the numbers in the boxes you should use Javascript. Remember that ASP.NET is a server side technology. Everything that ASP.NET does requires a page refresh, or an asynchronous update using AJAX. Neither are efficient for what you seem to want to do. Javascript is run client side. No refresh/server update required.

                Comment

                Working...