如何将字符串转换为时间跨度?视觉基础,秒表,膝部系统

时间:2018-11-30 06:10:35

标签: vb.net timespan stopwatch

我是初学者,正在使用Visual Studio 2012(Visual Basic)制作具有圈功能的秒表。现在我陷入了单圈功能。在膝部功能中,我想使用listview显示结果... 当用户第二次单击圈速按钮时,它必须显示第一次单击和第二次单击之间的时间间隔。但是我没有得到结果。这里有人解决我的问题吗?预先谢谢你 这是圈按钮事件的代码

Private Sub btnLap_Click(sender As Object, e As EventArgs) Handles btnLap.Click

    Dim lap As String = (sw.Elapsed.Hours.ToString("00") & ":" & sw.Elapsed.Minutes.ToString("00") & _
      ":" & sw.Elapsed.Seconds.ToString("00") & ":" & sw.Elapsed.Milliseconds.ToString("00"))

    lapcount += 1
    Dim i As Integer = ListView1.Items.Count
    If i <= 0 Then
        ListView1.Items.Add(CStr(lapcount), i)
        ListView1.Items(i).SubItems.Add(lap)
        ListView1.Items(i).SubItems.Add(lap)
    Else
        ListView1.Items.Add(CStr(lapcount), i)
        ListView1.Items(i).SubItems.Add(lap)
        ListView1.Items(i).SubItems.Add((TimeSpan.parse(lap)- Timespan.parse(lastlap)).ToString) ''' I can't subtract the value from 2nd click to first click

    End If
    lastlap = (ListView1.Items(i).SubItems(1).ToString)
End Sub

2 个答案:

答案 0 :(得分:0)

说实话,使用UI对象作为数据的主要存储区通常是一个坏主意。您最好将拆分时间存储在List (Of TimeSpan)中,并根据该计算来计算单圈时间。

在我的示例中,我声明了一些类对象,以便代码可以运行。您可以根据需要进行调整。

Dim sw As New Stopwatch
Dim lapcount As Integer = 1
Dim lastlap As Integer
Dim lapintervals As New List(Of TimeSpan)
Dim timeFormat As String = "hh\:mm\:ss\.fff"

因此,在您的按钮单击事件中,您将看到类似以下内容..

Private Sub BtnLap_Click(sender As Object, e As EventArgs) Handles BtnLap.Click
    Dim ts As TimeSpan = sw.Elapsed
    lapintervals.Add(ts)
    AddTimeToListView(lapintervals.Count, lapintervals.Last)
    lapcount += 1
End Sub

您将像这样将信息添加到ListView中

Private Sub AddTimeToListView(lapcount As Integer, lapsplit As TimeSpan)
    Dim lvItem As New ListViewItem(lapcount.ToString, lapcount)
    If lapcount = 1 Then
        lvItem.SubItems.Add(lapsplit.ToString(timeFormat))
        lvItem.SubItems.Add(lapsplit.ToString(timeFormat))
    Else
        lvItem.SubItems.Add(lapsplit.ToString(timeFormat))
        lvItem.SubItems.Add((lapsplit.Subtract(lapintervals(lapcount - 2))).ToString(timeFormat))
    End If
    ListView1.Items.Add(lvItem)
End Sub

答案 1 :(得分:0)

非常感谢您的回答... 终于我完成了秒表的申请.. 我做到了,我的领导者给了我10分中的8分。 这是设计表格和代码

visual basic 2012, stopwatch with lap button

enter image description here [代码]

公开课秒表     大众手表As Form'スタートページから呼び出すため     Dim sw As New System.Diagnostics.Stopwatch()

Dim lapcount As Integer
Dim lastlap As String

'スタート・ストップボタン
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    If btnStart.Text = "Start" Then
        sw.Start()     '時間計測開始
        Timer1.Interval = 10
        Timer1.Enabled = True
        btnStart.Text = "Stop"

    Else
        sw.Stop()   '時間計測一時停止
        Timer1.Enabled = False
        btnStart.Text = "Start"
    End If

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    lblTime.Text = sw.Elapsed.ToString("hh\:mm\:ss\:ff")

End Sub
'cancel / reset ボタン
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
    sw.Reset()     '初期状態にする
    Timer1.Enabled = False
    lblTime.Text = "00:00:00:00"
    ListView1.Items.Clear()
    btnStart.Text = "Start"
    lapcount = 0

End Sub

'ラップボタン
Private Sub btnLap_Click(sender As Object, e As EventArgs) Handles btnLap.Click

    Dim lap = sw.Elapsed.ToString("hh\:mm\:ss\:fff") ' ラップ時間を変数に代入

    lapcount += 1
    Dim i As Integer = ListView1.Items.Count
    If i <= 0 Then                                       ' ListviewのアイテムのValueが0または0より小さければ
        ListView1.Items.Add(CStr(lapcount), i)
        ListView1.Items(i).SubItems.Add(lap)                 'アイテムとサブアイテム表示
        ListView1.Items(i).SubItems.Add(lap)

    Else '文字列となっている結果時間を整数型に変換する/ // ラップ経過時間を取得するため
        Dim day As Integer = 0
        Dim hr As Integer = CInt(Strings.Left(lap, 2))
        Dim min As Integer = CInt(Strings.Mid(lap, 4, 2))
        Dim sec As Integer = CInt(Strings.Mid(lap, 7, 2))
        Dim milli As Integer = CInt(Strings.Mid(lap, 10, 3))

        Dim dayy As Integer = 0
        Dim hrr As Integer = CInt(Strings.Mid(lastlap, 19, 2))
        Dim minn As Integer = CInt(Strings.Mid(lastlap, 22, 2))
        Dim secc As Integer = CInt(Strings.Mid(lastlap, 25, 2))
        Dim millii As Integer = CInt(Strings.Mid(lastlap, 28, 3))

        '現在のラップ時間に直前に記憶したラップ時間を引き、1回目から2回目のラップボタン押下までの経過時間を図る
        Dim answer = createtimespan(day, hr, min, sec, milli) - createtimespan(dayy, hrr, minn, secc, millii)
        Dim ansTS = answer.ToString("hh\:mm\:ss\:fff") 'ミリ秒3桁まで表示するように指定

        ListView1.Items.Add(CStr(lapcount), i)
        ListView1.Items(i).SubItems.Add(lap)
        ListView1.Items(i).SubItems.Add((ansTS).ToString)  'ラップ時間の表示

    End If
    lastlap = ListView1.Items(i).SubItems(1).ToString   '直前のラップタイムを記憶
End Sub

Private Function createtimespan(ByVal days As Integer, ByVal hours As Integer, ByVal minutes As Integer, _
                                ByVal seconds As Integer, ByVal milliseconds As Integer) As TimeSpan

    Dim elapsedTime As New TimeSpan(days, hours, minutes, seconds, milliseconds)

    Return (elapsedTime)

End Function

结束班级