通过选定的组合框值迭代dateiff

时间:2014-01-30 22:00:50

标签: vb.net

我遇到了一些逻辑上的问题,并想知道你是否能够帮助我度过难关?

如何遍历组合框以使datediff函数正常工作。

我正在尝试计算组合框的时差,cbIn(i)vs clout(i)

Dim HoursBox1 As List(Of ComboBox)

Private Sub TimeSheet_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


HoursBoxIn1 = New List(Of ComboBox)(New ComboBox() {cbInW1, cbInTh1, cbInF1, cbInSat1, cbInSun1, cbInMon1, cbInTue1})
    HoursBoxOut1 = New List(Of ComboBox)(New ComboBox() {cbOutW1, cbOutTh1, cbOutF1, cbOutSat1, cbOutSun1, cbOutMon1, cbOutTue1})


End Sub





Private Sub btnCalcTimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcTimes.Click


For Each cb As ComboBox In HoursBox1

         theTime = (DateDiff(DateInterval.Minute, cbInW1.SelectedItem,   cbOutW1.SelectedItem) / 60)

next
     For Each cb As ComboBox In HoursBoxOut1


        Next


End Sub

1 个答案:

答案 0 :(得分:0)

要扩展silvusvalentine的答案,因为您按分钟计算并除以60,您可能希望将类型更改为十进制。我假设您还想要添加列表并进行一些错误处理,以防有人没有选择值。

    times = New List(Of Decimal)
    Dim ListSum As Decimal = 0
    For i As Integer = 0 To HoursBoxIn1.Count - 1

' if the item is empty add a decimal 

    If String.IsNullOrEmpty(HoursBoxIn1(i).SelectedItem) Then
            times.Add(0)
        Else
            times.Add(DateDiff(DateInterval.Minute, DateTime.Parse(HoursBoxIn1(i).SelectedItem), DateTime.Parse(HoursBoxOut1(i).SelectedItem)) / 60)
        End If
    Next

' get a total for the hours in the list of times

    For Each t As Decimal In times
        ListSum = ListSum + Val(t)
    Next
 Msgbox("Total of hours in the list " & ListSum)