How to add numbers in a textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ITprogramer17
    New Member
    • Oct 2007
    • 1

    How to add numbers in a textbox

    Hi...
    How to add 4+6 when you use two textbox, the first textbox is for the input and the other one if for the output..
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    As you have posted a question in the articles section it is being moved to Visual Basic Forum.

    MODERATOR.

    Comment

    • creative1
      Contributor
      • Sep 2007
      • 274

      #3
      Spit the string(4+6) and store in two variables. then add them and display result in other textbox.

      Comment

      • jrtox
        New Member
        • Sep 2007
        • 89

        #4
        What if 2+3+3-4.?
        Very wise using 1 txtbox only.

        Comment

        • Ali Rizwan
          Banned
          Contributor
          • Aug 2007
          • 931

          #5
          Originally posted by ITprogramer17
          Hi...
          How to add 4+6 when you use two textbox, the first textbox is for the input and the other one if for the output..
          Try this code

          Code:
          Private Sub command1_click()
          
          text2.text= val(text2) + val(text1)
          text1.text=""
          
          end sub
          Now every time you enter a value in text1 it will added to previous one.
          This code is for Adding.
          You can made for subtraction yourself.

          GOODLUCK
          ALI

          Comment

          • jrtox
            New Member
            • Sep 2007
            • 89

            #6
            Originally posted by Ali Rizwan
            Try this code

            Code:
            Private Sub command1_click()
            
            text2.text= val(text2) + val(text1)
            text1.text=""
            
            end sub
            Now every time you enter a value in text1 it will added to previous one.
            This code is for Adding.
            You can made for subtraction yourself.

            GOODLUCK
            ALI

            That will do,
            but as i understand the post.
            the problem is, Text1 is only for Input like 6+4.
            and and text2 is for output like the answer 10.

            not text1.text which is 6 plus(+) text2.text which is 4

            Comment

            • Ariharan
              New Member
              • Aug 2007
              • 20

              #7
              Originally posted by ITprogramer17
              Hi...
              How to add 4+6 when you use two textbox, the first textbox is for the input and the other one if for the output..
              Tell your problem clearly.
              How will you give input?

              If you give the input 4 in Text1 and 6 in Text2 following code works well
              Code:
              Text1.Text=val(Text1.Text)+val(text2.text)

              If you give the input 4+6 in Text1 and nothing in Text2 then following code works well
              Code:
              i=1
              c=0
              Do While c=0
              If (i=Len(Trim(Text1)) Then
                 Exit Do
              End If
              If (mid(Text1,i,1)='+' ) Then
                 Text2=val(mid(Text1,1,i-1))+val(mid(Text1,i+1)
                 c=1
              End If
              i=i+1
              Loop
              Waiting for ur reply

              Comment

              • Kosmos
                New Member
                • Sep 2006
                • 153

                #8
                Just checking...you' re talking about VBA correct? or actual VB?

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #9
                  Originally posted by ITprogramer17
                  Hi...
                  How to add 4+6 when you use two textbox, the first textbox is for the input and the other one if for the output..
                  write a code that searches character by character for numbers and signs, the easiest example would be with a couple of arrays something like this

                  [CODE=vb]public function Maths(byval Str1 as string) as double
                  dim i as integer
                  dim j as integer
                  dim k as integer
                  dim Int1 as integer
                  dim Str2 as string
                  dim Arr1() as double 'lets store here the numbers
                  dim Arr2() as integer 'and here the operators (1 for sum, 2 for diference)
                  j=0
                  k=0
                  redim arr1( j )
                  for i = 1 to len(str1)
                  int1=asc(mid(st r1,i,1))
                  if (int1 >= 48 and int1 <= 57) or int1 = 46 then str2 = str2 & chr(int1)
                  if int1= 43 or int1 = 45 then
                  redim preserve arr2 (k)
                  arr2(k) = int(int1/2)-20
                  arr1(j) = str2
                  str2 = ""
                  k=k+1
                  j=j+1
                  redim preserve arr1(j)
                  end if
                  next
                  arr1(j) = str2
                  maths = arr1(0)
                  for i = 1 to ubound(arr1)
                  if arr2(i-1) = 1 then
                  maths = maths + arr1(i)
                  else
                  maths = maths - arr1(i)
                  end if
                  next
                  end function[/CODE]

                  That'll be the main idea
                  Just make sure not to use negative numbers HTH

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    I believe you can also use the Microsoft Script Control to evaluate an expression in a string, like that. Try a search here, as it has been covered before.

                    Comment

                    • Ariharan
                      New Member
                      • Aug 2007
                      • 20

                      #11
                      Originally posted by Kosmos
                      Just checking...you' re talking about VBA correct? or actual VB?
                      Shall i know whats VBA???

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by Ariharan
                        Shall i know whats VBA???
                        Visual Basic is a complete development environment and language, including a compiler to produce applications in Exe form, or DLLs.

                        Visual Basic for Applications (VBA) is a "trimmed down" version of the language built into various products (possibly just the MS Office product?) as a scripting language.

                        Comment

                        Working...