Help needed
I would like to know how to sum seven text boxes with time in vb.net.
Anybody can help me please?
Thanks

Hi,
You can use following code.

'Declare a Date Time Variable
        Dim TempDateTime As DateTime = Nothing
        'Declare a local time span variable
        Dim TempTimeSpan As New TimeSpan

        'Declare a arry of type string and set the size equal to number of text boxes.
        Dim arr(4) As String
        'set the value for text boxs to array
        arr(0) = TextBox1.Text
        arr(1) = TextBox2.Text
        arr(2) = TextBox3.Text
        arr(3) = TextBox4.Text
        arr(4) = TextBox5.Text

        For i As Integer = 0 To arr.Length - 1
            TempDateTime = CDate(arr(i))
            TempTimeSpan = TempTimeSpan.Add(New TimeSpan(TempDateTime.Hour, TempDateTime.Minute, 0))
        Next
        'showing the total time.
        MessageBox.Show("HH: " & TempTimeSpan.Hours & " MM: " & TempTimeSpan.Minutes)

Happy Coding:-)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "3:05"
        TextBox2.Text = "1:00"
        TextBox3.Text = "0:55"
        Dim span As New TimeSpan
        span = TimeSpan.Parse(TextBox1.Text) + TimeSpan.Parse(TextBox2.Text) + TimeSpan.Parse(TextBox3.Text)
        TextBox4.Text = span.ToString
    End Sub

Thanks for your prompt reply.
I tryed to use the code but it doesn't work or i am use it
wrongly.
Can you please help me to go through it?
Thanks

Public Class Form1

   Private Sub PayrollTimeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayrollTimeBindingNavigatorSaveItem.Click
      Me.Validate()
      Me.PayrollTimeBindingSource.EndEdit()
      Me.PayrollTimeTableAdapter.Update(Me.PayrollTimeDataSet.PayrollTime)

   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'TODO: This line of code loads data into the 'PayrollTimeDataSet.PayrollTime' table. You can move, or remove it, as needed.
      Me.PayrollTimeTableAdapter.Fill(Me.PayrollTimeDataSet.PayrollTime)

   End Sub

   Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click
      Dim TimeA As Date
      Dim TimeB As Date
      Dim hh As Integer
      Dim mm As Integer
      Dim ss As Integer

      'Calculation for Monday
      If Not Date.TryParse(MonInTextBox.Text, TimeA) Then
      End If
      If Not Date.TryParse(MonOutTextBox.Text, TimeB) Then
      End If
      hh = TimeB.Subtract(TimeA).Hours
      mm = TimeB.Subtract(TimeA).Minutes
      ss = TimeB.Subtract(TimeA).Seconds
      MonTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))

      'Calculation for Tuesday
      Dim TimeC As Date
      Dim TimeD As Date

      If Not Date.TryParse(TueInTextBox.Text, TimeC) Then
      End If
      If Not Date.TryParse(TueOutTextBox.Text, TimeD) Then
      End If
      hh = TimeD.Subtract(TimeC).Hours
      mm = TimeD.Subtract(TimeC).Minutes
      ss = TimeD.Subtract(TimeC).Seconds
      TueTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))

      'Calculation for Wednesday
      Dim TimeE As Date
      Dim TimeF As Date

      If Not Date.TryParse(WedInTextBox.Text, TimeE) Then
      End If
      If Not Date.TryParse(WedOutTextBox.Text, TimeF) Then
      End If
      hh = TimeF.Subtract(TimeE).Hours
      mm = TimeF.Subtract(TimeE).Minutes
      ss = TimeF.Subtract(TimeE).Seconds
      WedTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))

      'Calculation for Thursday
      Dim TimeG As Date
      Dim TimeH As Date

      If Not Date.TryParse(ThuInTextBox.Text, TimeG) Then
      End If
      If Not Date.TryParse(ThuOutTextBox.Text, TimeH) Then
      End If
      hh = TimeH.Subtract(TimeG).Hours
      mm = TimeH.Subtract(TimeG).Minutes
      ss = TimeH.Subtract(TimeG).Seconds
      ThuTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))

      'Calculation for Friday
      Dim TimeI As Date
      Dim TimeJ As Date

      If Not Date.TryParse(FriInTextBox.Text, TimeI) Then
      End If
      If Not Date.TryParse(FriOutTextBox.Text, TimeJ) Then
      End If
      hh = TimeJ.Subtract(TimeI).Hours
      mm = TimeJ.Subtract(TimeI).Minutes
      ss = TimeJ.Subtract(TimeI).Seconds
      FriTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))

      'Calculation for Saturday
      Dim TimeK As Date
      Dim TimeL As Date

      If Not Date.TryParse(SatInTextBox.Text, TimeK) Then
      End If
      If Not Date.TryParse(SatOutTextBox.Text, TimeL) Then
      End If
      hh = TimeL.Subtract(TimeK).Hours
      mm = TimeL.Subtract(TimeK).Minutes
      ss = TimeL.Subtract(TimeK).Seconds
      SatTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))

      'Calculation for Sunday
      Dim TimeM As Date
      Dim TimeN As Date

      If Not Date.TryParse(SunInTextBox.Text, TimeM) Then
      End If
      If Not Date.TryParse(SunOutTextBox.Text, TimeN) Then
      End If
      hh = TimeN.Subtract(TimeM).Hours
      mm = TimeN.Subtract(TimeM).Minutes
      ss = TimeN.Subtract(TimeM).Seconds
      SunTotalTextBox.Text = hh.ToString.PadLeft(2, CChar("0")) & ":" & mm.ToString.PadLeft(2, CChar("0")) & ":" & ss.ToString.PadLeft(2, CChar("0"))


      'Calculate the total of hours worked
      'Declare a Date Time Variable        
      Dim TempDateTime As DateTime = Nothing
      'Declare a local time span variable
      Dim TempTimeSpan As New TimeSpan

      'Declare a array of type string and set the size equal to number of text boxes.
      Dim arr(6) As String
      'set the value for text boxs to array
      arr(0) = MonTotalTextBox.Text
      arr(1) = TueTotalTextBox.Text
      arr(2) = WedTotalTextBox.Text
      arr(3) = ThuTotalTextBox.Text
      arr(4) = FriTotalTextBox.Text
      arr(5) = SatTotalTextBox.Text
      arr(6) = SunTotalTextBox.Text

      For i As Integer = 0 To arr.Length - 1
         TempDateTime = CDate(arr(i))
         TempTimeSpan = TempTimeSpan.Add(New TimeSpan(TempDateTime.Hour, TempDateTime.Minute, 0))
      Next
      'showing the total time.
      TotalHoursTextBox.Text = (TempTimeSpan.Hours & ":" & TempTimeSpan.Minutes)


      'Calculate the overtime
      Dim differenceHour As DateTime
      If OverTHoursTextBox.Text = 1 Then
         OverTHoursTextBox.Text = Convert.ToInt32(differenceHour.Hour)

      End If
      ' If the employee worked over 40 hours, there is overtime
      If TotalHoursTextBox.Text >= 40 Then
         RegHoursTextBox.Text = 40
         OverTHoursTextBox.Text = TotalHoursTextBox.Text - RegHoursTextBox.Text

         ' If the employee worked under 40 hours, calculate the overtime
      ElseIf TotalHoursTextBox.Text < 40 Then
         RegHoursTextBox.Text = 0
         OverTHoursTextBox.Text = 0

      End If

      Me.RegHoursTextBox.Text = RegHoursTextBox.Text.ToString("F")
      Me.OverTHoursTextBox.Text = OverTHoursTextBox.Text.ToString("F")


   End Sub
End Class

Edited 7 Years Ago by __avd: Add [code] tags. Encase your code in: [code] and [/code]

try this calculation :

'Calculation for Monday
        If Not Date.TryParse(MonInTextBox.Text, TimeA) Then
        End If
        If Not Date.TryParse(MonOutTextBox.Text, TimeB) Then
        End If
        Dim span As New TimeSpan
        span = TimeSpan.Parse(TimeB.ToShortTimeString) - TimeSpan.Parse(TimeA.ToShortTimeString)
        MonTotalTextBox.Text = span.ToString
span = TimeSpan.Parse(TimeB.ToShortTimeString) - TimeSpan.Parse(TimeA.ToShortTimeString)

When i tried to apply the above code it comes a message saying hat:
Input string was not in a correct format.

Edited 7 Years Ago by __avd: Add [code] tags. Encase your code in: [code] and [/code]

yea that why i asked you for some example content of your textboxes.
how you enter the time? like "10:30" or "10.30" or whatever.
the code i provided is working with the format hh:mm(.ss )

then attach your project and we can check it for you. its hard to find the problem if you dont really answer the questions (like how you format the textboxes)

Thanks for your help.
I was about to give up with this project.
I am using sql 2005 database and the data type i am using is varchar(50)
Thanks

Hello.....I'm doing Fingerprint based staff attendance project in vb.net

and I want some codings for time.

(eg) Time calculation and check his\her attendance time.

please people help me.........

This article has been dead for over six months. Start a new discussion instead.