Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
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
VB
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
Updated 28-Dec-12 5:32am
v4

VB
For Each digit In (TextBox4.Text + TextBox5.Text + TextBox6.Text).ToCharArray()
     i += CInt(digit)

Convert it to a CharArray first
 
Share this answer
 
I would create a function, that took a string as it's parameter, and returned a value that was the digits added together.
VB
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:
VB
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
 
Share this answer
 
Hi,

Try this:
VB
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.
 
Share this answer
 
v4
Comments
[no name] 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!!!!!!!
Thomas Daniels 28-Dec-12 12:14pm    
I updated my answer.
[no name] 28-Dec-12 19:03pm    
Thanks my Problem Solved
Thomas Daniels 29-Dec-12 3:34am    
You're welcome!
Try creating a function that will add the values. You can change the code below to what you want.

VB
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
 
Share this answer
 
v2
VB
'
' 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
 
Share this answer
 
Comments
Akinmade Bond 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900