Sum of the numbers of one textbox

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 18
Reputation: Lida_pink is an unknown quantity at this point 
Solved Threads: 0
Lida_pink Lida_pink is offline Offline
Newbie Poster

Sum of the numbers of one textbox

 
0
  #1
Oct 25th, 2009
How can I get the sum of the numbers of only one textbox?
For example text1.text= 1367
How to get the sum of this 4 digit number?

I appreciate your assistance in advance.
Lida
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: turseno is an unknown quantity at this point 
Solved Threads: 1
turseno turseno is offline Offline
Newbie Poster
 
0
  #2
Oct 25th, 2009
Originally Posted by Lida_pink View Post
How can I get the sum of the numbers of only one textbox?
For example text1.text= 1367
How to get the sum of this 4 digit number?

I appreciate your assistance in advance.
Lida
Dear Lida,

Try this:

text1.text = 1367
text2.text = sum(text1.text)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 505
Reputation: abu taher is an unknown quantity at this point 
Solved Threads: 25
abu taher's Avatar
abu taher abu taher is offline Offline
Posting Pro
 
0
  #3
Oct 26th, 2009
if you want only 4 digit will type in text box then adjust the properties of text box. max length = 4
I like sword. Attack or Defense.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 10
Reputation: Celt.Max is an unknown quantity at this point 
Solved Threads: 4
Celt.Max Celt.Max is offline Offline
Newbie Poster

To all...

 
0
  #4
Oct 26th, 2009
I'm not sure if the solution is so easy as you write... VB(at least my 6 one) does not have the sum function... its necessary to write it at first. It could looks like this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function SUM(sNumber as String) As Integer
  2. dim iX as integer, iCount as integer
  3. for iX = 1 to len(sNumber)
  4. iCount = iCount + CInt(Mid(sNumber, iX, 1))
  5. Next iX
  6. SUM = iCount
  7. End Function

Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Lida_pink is an unknown quantity at this point 
Solved Threads: 0
Lida_pink Lida_pink is offline Offline
Newbie Poster
 
0
  #5
Oct 26th, 2009
Originally Posted by turseno View Post
Dear Lida,

Try this:

text1.text = 1367
text2.text = sum(text1.text)
Unfortunately this also doesn't work....I've already tried it..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Lida_pink is an unknown quantity at this point 
Solved Threads: 0
Lida_pink Lida_pink is offline Offline
Newbie Poster
 
0
  #6
Oct 26th, 2009
Originally Posted by Celt.Max View Post
I'm not sure if the solution is so easy as you write... VB(at least my 6 one) does not have the sum function... its necessary to write it at first. It could looks like this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function SUM(sNumber as String) As Integer
  2. dim iX as integer, iCount as integer
  3. for iX = 1 to len(sNumber)
  4. iCount = iCount + CInt(Mid(sNumber, iX, 1))
  5. Next iX
  6. SUM = iCount
  7. End Function

This one doesn't work either....
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 914
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 167
vb5prgrmr vb5prgrmr is offline Offline
Posting Shark
 
0
  #7
Oct 26th, 2009
Lida_Pink,

Lets make sure of our definitions first... The sum of numbers is, those numbers being added together, while the product of numbers is, those numbers being multiplied together. So if you want the sum of numbers in a string then celt.max's code does work...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4. Dim Result As Integer
  5. Result = SUM("1234")
  6. MsgBox Result
  7. End Sub
  8.  
  9. Public Function SUM(sNumber As String) As Integer
  10. Dim iX As Integer, iCount As Integer
  11. For iX = 1 To Len(sNumber)
  12. iCount = iCount + CInt(Mid(sNumber, iX, 1))
  13. Next iX
  14. SUM = iCount
  15. End Function

However, if you are wanting the product of the numbers, then yes a different solution is needed. So, are you wanting the product of these numbers?



Good Luck
If anyone has helped you solve your problem, please mark your thread as solved.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 255
Reputation: AndreRet is an unknown quantity at this point 
Solved Threads: 38
AndreRet AndreRet is offline Offline
Posting Whiz in Training
 
0
  #8
Oct 26th, 2009
Try the following to get the sum of an integer -

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. On Error GoTo Errorhandler
  3. x = InputBox("Please Enter an Integer.", , Text1.Text)
  4. If x <> "" Then
  5. List1.AddItem Str(x)
  6. Do While x <> 1
  7. If x Mod 2 = 0 Then
  8. x = x / 2
  9. Else
  10. x = (x * 3) + 1
  11. End If
  12. List1.AddItem Str(x)
  13. Loop
  14. 'Number of steps used
  15. Label1 = List1.ListCount - 1
  16. End If
  17. Errorhandler:
  18. Exit Sub
  19. End Sub
  20.  
  21. 'Even numbers are divided by 2
  22. 'Odd numbers are multiplied by 3 and are then increased by 1
  23. 'The problem ends by reaching the number 1
Please mark questions as answered when done.

Be the ONE!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Lida_pink is an unknown quantity at this point 
Solved Threads: 0
Lida_pink Lida_pink is offline Offline
Newbie Poster
 
0
  #9
Oct 26th, 2009
I need the sum of the numbers. And thank you very much. Your answer is correct.
Cheers,
Lids
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 556 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC