11,926,933 members (46,303 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i made a text 3 text boxes and one button in that three text boxes i enter

DOB like ex:21 07 1993 format in textboxes 4,5,6 i will get out put as 2+1+0+7+1+9+9+3=34>3+4=7 in that means 7 will come as output in 7th text box. my problem if enter b'day like :1 3 1986 > 1+3+1+9+8+6=28 > 2+8=10 here "10" is displayed in 7th text box. but i want output as 1+0= 1 i.e "1" as output(in case to two digit numbers) how can i do that please tell me

i tried this
```Private Sub KryptonButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton1.Click
Dim i As Integer = 0
Dim l As Integer = 0
Dim lcn As String
If TextBox4.Text = "" And TextBox5.Text = "" Then
MessageBox.Show("Please Enter Date of Birth!!!")
End If
For Each digit In (TextBox4.Text + TextBox5.Text + TextBox6.Text)
i += Val(digit)
Next
lcn = i.ToString
For Each digit In lcn
l += Val(digit)
Next
TextBox7.Text = l.ToString

End Sub```

please help me read carefully once again before answering thanks to all:Merry Christmas!!!
Posted 28-Dec-12 6:26am
Edited 28-Dec-12 6:32am
v4

## 5 solutions

Rate this:
Please Sign up or sign in to vote.

## Solution 1

```For Each digit In (TextBox4.Text + TextBox5.Text + TextBox6.Text).ToCharArray()
i += CInt(digit)
```
Convert it to a CharArray first
Rate this:
Please Sign up or sign in to vote.

## Solution 2

I would create a function, that took a string as it's parameter, and returned a value that was the digits added together.
```Private Function AddDigits(s As String) As Integer
Dim result As Integer = 0
For Each c As Char In s
If [Char].IsDigit(c) Then
result += AscW(c - "0"C)
End If
Next
Return result
End Function```
I would then use a loop to call this until I had only one digit left:
```Dim str As String = "123324::34"
Dim res As Integer = 0
Do
res = AddDigits(str)
str = res.ToString()
Loop While res >= 10
myTextBox.Text = str```
Rate this:
Please Sign up or sign in to vote.

## Solution 3

Hi,

Try this:
```Private Sub KryptonButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonButton1.Click
Dim i As Integer = 0
Dim l As Integer = 0
Dim lcn As String = ""
If String.IsNullOrEmpty(textBox4.Text) AndAlso String.IsNullOrEmpty(textBox5.Text) AndAlso String.IsNullOrEmpty(textBox6.Text) Then
MessageBox.Show("Please Enter Date of Birth!!!")
Return
End If
Dim dateOfBirthWithoutSpaces As String = (TextBox4.Text + TextBox5.Text + TextBox6.Text).Replace(" ", "")
For Each c As Char In dateOfBirthWithoutSpaces
i += (Convert.ToInt32(c.ToString()))
Next
lcn = i.ToString()
While lcn.Length > 1
l = 0
For Each c As Char In lcn
l += (Convert.ToInt32(c.ToString()))
Next
lcn = l.ToString()
End While
TextBox7.Text = lcn
End Sub
```
Hope this helps.
v4
Comments
charuwaka 28-Dec-12 12:07pm

Dear ProgramFOX,

Your answer my answer produce same result no change !!!

what i want is if i enter date like in three text boxes "01 03 1986" according you & i solution is "10" but i want 1+0= "1" i.e i want 1 as out put in case of two digit please help me ex: 23 07 1993 output is "7" this OK i want answer in case of two digit!!!!!!!
ProgramFOX 28-Dec-12 12:14pm

I updated my answer.
charuwaka 28-Dec-12 19:03pm

Thanks my Problem Solved
ProgramFOX 29-Dec-12 3:34am

You're welcome!
Rate this:
Please Sign up or sign in to vote.

## Solution 4

Try creating a function that will add the values. You can change the code below to what you want.

```Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Day As String = "1"
Dim Month As String = "3"
Dim Year As String = "1986"
Dim Holder As Integer = 0

For Each num As Char In (Day + Month + Year)
Holder += Val(num)
Next

MessageBox.Show(AddString(Holder))
End Sub

Function AddString(number As String) As String
Dim i As Integer = 0

For Each num As Char In number
i += Val(num)
Next

If i >= 10 Then
Return AddString(i)
End If

Return i.ToString()
End Function
```
v2
Rate this:
Please Sign up or sign in to vote.

## Solution 5

```'
' This is a Very Simple Solution applicable for any Number, It Based on Simple Maths
' It so Easy, No need to Work with Loops or Goto's
'
' Any Number (Mod) 9 = Sum Of Digits
'
' 12 Mod 9 = 3
' 21 Mod 9 = 3
' 28 Mod 9 = 1 (2+8 = 1+0 = 1)
' One Validation of Any Number / 9 = 0 then Sum of Digit is 9
' 27 Mod 9 = 0
' Hence Sum of Digit is 9
'
Function SumOfDigits(DOB As Long) As Integer
'One Line Calculation
SumOfDigits =  DOB Mod 9

'One more Validation
If SumOfDigits = 0 AND DOB <> 0 Then SumOfDigits = 9

'Return Result
Return SumOfDigits
End Function
```
Comments
GeekBond 29-Dec-12 6:06am

Nice work. +5
Ashok19r91d 29-Dec-12 6:07am

Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 200 Sergey Alexandrovich Kryukov 159 KrunalRohit 155 F-ES Sitecore 155 Jochen Arndt 150
 OriginalGriff 7,546 KrunalRohit 4,693 Sergey Alexandrovich Kryukov 3,654 George Jonsson 2,935 Suvendu Shekhar Giri 2,271

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100