Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have Three Textbox
One Year 1 another one Year 2 and other textbox total Year
I give the input Year 1 textbox value 2.3
and Year 2 textbox value 2.8 then
I need output is in total Year textbox 4.11
How to do this calculation any one suggest Me
Posted
Updated 20-Aug-14 4:31am
v5
Comments
Pheonyx 20-Aug-14 10:04am    
[Scrap that I miss read your question, you need to explain your addition and how you get your answer] Let me understand this, you simple mean you have two text boxes and you want to add their values together and put them in a third?
This is basic, you should be able to do this.
Richard Deeming 20-Aug-14 10:06am    
Not that basic, if 2.3 + 2.8 = 4.11!
Pheonyx 20-Aug-14 10:09am    
Haha, my bad, I think it was the way the question was written I read it and my head said they add up. Cheers for pointing that out. :-)
Richard Deeming 20-Aug-14 10:06am    
Without knowing how you arrived at the magic number "4.11" given the two input numbers "2.3" and "2.8", there's no way anyone can help you.
hebsiboy 20-Aug-14 10:28am    
This is year Calculation 2.3 is year one and 2.8 is year two i need sum of year that is 4.11

VB
Dim arr1() As String
Dim arr2() As String
Dim intY As Integer
Dim intM As Integer
' Remove extraneous spaces
TextBox1.Text = TextBox1.Text.Trim
' Ensure that there is a period
If Not TextBox1.Text.Contains(".") Then
    TextBox1.Text += "."
End If
' Remove extraneous spaces
TextBox2.Text = TextBox2.Text.Trim
' Ensure that there is a period
If Not TextBox2.Text.Contains(".") Then
    TextBox2.Text += "."
End If
' Split into elements of an array (Index 0=Years, Index 1=Months)
arr1 = TextBox1.Text.Split(CChar("."))
arr2 = TextBox2.Text.Split(CChar("."))
' Convert to Integers and sum the values
Try
    intY = CInt(arr1(0)) + CInt(arr2(0))
    intM = CInt(arr1(1)) + CInt(arr2(1))
Catch ex As Exception
    ' Integer conversion failed for one of the values
    MessageBox.Show("One of the inputs is not a numeric", "Error", _
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
    ' If the months value is greater or equal to 12
    ' Adjust the months value and the year value
    If intM > 11 Then
        intY += intM \ 12
        intM -= (intM \ 12) * 12
    End If
    ' Assign the new computed value to the TextBox
    TextBox3.Text = Format(intY, "###") & "." & Format(intM, "##")
End Try
 
Share this answer
 
v2
Use double.TryParse[^] to convert teh textbox.Text value to a numeric value.
Add then together, and set the new text box Text property using ToString.
 
Share this answer
 
If I understand your requirement correctly
Without any validation or declarations:

Pos = InStr(Textbox1.Text,".")
y1 = Val(left(Textbox1.Text, Pos - 1))
m1 = Val(mid(Textbox1.Text, Pos + 1))
Pos = InStr(Textbox2.Text,".")
y2 = Val(left(Textbox2.Text, Pos - 1))
m2 = Val(mid(Textbox2.Text, Pos + 1))

Textbox3.Text = (y1 + y2) & "." & (m1 + m2)

What happens if m1 + m2 is more than 12? If you want this turned into years/months, you could use integer dicision to get the div and mod and add the div to y1 and y2.
 
Share this answer
 
v2

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