Answered # Sum of the numbers of one textbox

turseno abu taher 4 Celt.Max 1 Discussion Starter Lida_pink Discussion Starter Lida_pink vb5prgrmr 143 AndreRet 362 Discussion Starter Lida_pink Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

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)

0

if you want only 4 digit will type in text box then adjust the properties of text box. max length = 4

0

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:

```
Public Function SUM(sNumber as String) As Integer
dim iX as integer, iCount as integer
for iX = 1 to len(sNumber)
iCount = iCount + CInt(Mid(sNumber, iX, 1))
Next iX
SUM = iCount
End Function
```

:)

0

Dear Lida,

Try this:

text1.text = 1367

text2.text = sum(text1.text)

Unfortunately this also doesn't work....I've already tried it.. :(:(

0

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:

`Public Function SUM(sNumber as String) As Integer dim iX as integer, iCount as integer for iX = 1 to len(sNumber) iCount = iCount + CInt(Mid(sNumber, iX, 1)) Next iX SUM = iCount End Function`

:)

This one doesn't work either.... :(

0

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

```
Option Explicit
Private Sub Form_Load()
Dim Result As Integer
Result = SUM("1234")
MsgBox Result
End Sub
Public Function SUM(sNumber As String) As Integer
Dim iX As Integer, iCount As Integer
For iX = 1 To Len(sNumber)
iCount = iCount + CInt(Mid(sNumber, iX, 1))
Next iX
SUM = iCount
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

0

Try the following to get the sum of an integer -

```
Private Sub Command1_Click()
On Error GoTo Errorhandler
x = InputBox("Please Enter an Integer.", , Text1.Text)
If x <> "" Then
List1.AddItem Str(x)
Do While x <> 1
If x Mod 2 = 0 Then
x = x / 2
Else
x = (x * 3) + 1
End If
List1.AddItem Str(x)
Loop
'Number of steps used
Label1 = List1.ListCount - 1
End If
Errorhandler:
Exit Sub
End Sub
'Even numbers are divided by 2
'Odd numbers are multiplied by 3 and are then increased by 1
'The problem ends by reaching the number 1
```

0

I need the sum of the numbers. And thank you very much. Your answer is correct.

Cheers,

Lids

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```