How can I add a timer in the listview that minuses 1 second at a time when the button starts click and stops when the stop button is clicked? Then save the remaining time in the database. Please help me in my school project. Thanks in advance.

I prof. says that he want to view in the list box is like this 10:00 to 9:59 to 9:58 etc.
there is a default 10:00 in the listview.

Here is the sample screenshot

http://www.daniweb.com/forums/attachment.php?attachmentid=21484&stc=1&d=1309667451

Recommended Answers

All 17 Replies

See if this helps.

(1 Timer)

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With Timer1
            .Interval = 1000 '// set interval to 1,000, which is 1 second.
            .Start() '// start the Timer.
        End With
    End Sub

    Private arTimer() As String = Nothing

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items in ListView.
            With itm.SubItems(4) '// Get column 5.
                If .Text.Contains(":") Then '// if it .Contains the ":".
                    arTimer = .Text.Split(":"c) '// .Split it into 2 Arrays.
                    If arTimer(1) = "00" Then '// check Array 2 for "00".
                        If CInt(arTimer(0)) > 0 Then '// if minutes are at "00" and Array 1 is greater than 0, the hours,
                            arTimer(1) = "59" '// set seconds to 59, and
                            arTimer(0) = CStr(CInt(arTimer(0)) - 1) '// subtract hour.
                        End If
                    End If
                    If Not .Text = "0:00" Then '// check if not at "0:00".
                        arTimer(1) = CStr(CInt(arTimer(1)) - 1) '// subtract 1 second from seconds.
                        If arTimer(1).Length = 1 Then arTimer(1) = "0" & arTimer(1) '// add a "0" if seconds are from 0-9.
                        .Text = arTimer(0) & ":" & arTimer(1) '// set .Text back to ListViewItem.
                    End If
                End If
            End With
        Next
    End Sub
End Class

Thank you very much sir codeorder. one more question sir, what if i want to start the time in one user only? because when i add a user in my listview and click the start button both of them start their time. how can only start one user only?

and when i select the other user and start their time the first one that i start is continues deducting a time.

And how can i save it to database.

Saving to db(database), not a db coder here. Good luck with that.

As for start/stop time of individual users, I ended up using the .Tag Property of each ListViewItem added. The .Tag property is a hidden property which can be used for such cases as this. Each time you add an item to the ListView, you set the .Tag to something. If it's the correct .Tag, then the Timer will subtract time from that item.

See if this helps.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With ListView1 '// setup ListView.
            .Columns.Add("Column 1", 100) : .Columns.Add("Column 2", 100) : .Columns.Add("Column 3", 100) : .Columns.Add("Column 4", 100) : .Columns.Add("Column Time", 100) : .View = View.Details : .FullRowSelect = True
        End With
        With Timer1
            .Interval = 1000
            .Start()
        End With
        Button1.Text = "Add User" : Button2.Text = "Start/Stop"
    End Sub

    Private arTimer() As String = Nothing '// Array to .Split time into hours/seconds.

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items in ListView.
            '///////////////////////////////
            If itm.Tag.ToString = "start countdown" Then
                '////////////////////
                With itm.SubItems(4) '// Get column 5.
                    If .Text.Contains(":") Then '// if it .Contains the ":".
                        arTimer = .Text.Split(":"c) '// .Split it into 2 Arrays.
                        If arTimer(1) = "00" Then '// check Array 2 for "00".
                            If CInt(arTimer(0)) > 0 Then '// if minutes are at "00" and Array 1 is greater than 0, the hours,
                                arTimer(1) = "59" '// set seconds to 59, and
                                arTimer(0) = CStr(CInt(arTimer(0)) - 1) '// subtract hour.
                            End If
                        End If
                        If Not .Text = "0:00" Then '// check if not at "0:00".
                            arTimer(1) = CStr(CInt(arTimer(1)) - 1) '// subtract 1 second from seconds.
                            If arTimer(1).Length = 1 Then arTimer(1) = "0" & arTimer(1) '// add a "0" if seconds are from 0-9.
                            .Text = arTimer(0) & ":" & arTimer(1) '// set .Text back to ListViewItem.
                        End If
                    End If
                End With
            End If
        Next
    End Sub

    '// Add users.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Static iNext As Integer = 1 '// for user #.
        Dim sTemp As String = "user " & iNext.ToString & ",subitem,subitem 2,subitem 3,10:00"
        Dim lvItem As New ListViewItem(sTemp.Split(CChar(",")))
        lvItem.Tag = "stop countdown" '// set to "stop ..." to not automatically start the countdown.
        ListView1.Items.Add(lvItem)
        iNext += 1 '// set # for next user.
        ListView1.Select()
    End Sub

    '// Start/Stop time for each individual user.
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If Not ListView1.SelectedItems.Count = 0 Then '// check if item is selected.
            With ListView1.SelectedItems(0)
                If .Tag.ToString = "start countdown" Then .Tag = "stop countdown" Else .Tag = "start countdown" '// toggle start/stop of countdown.
            End With
            ListView1.Select()
        End If
    End Sub
End Class

And how can i save it to database.

When do you want to save it, where and using what (as a column and as a field) as criteria for the update. If you want to create a new record (and not update an existing one) you'll need to provide with what you want to insert as a new record (I'm guessing fields from the listview, so provide both fieldnames and columns in your listview)

Edit: Even better, provide the SQL statement of what you want done and info on where you want it to execute. (In the spirit of we help those that help themselves).

Saving to db(database), not a db coder here. Good luck with that.

As for start/stop time of individual users, I ended up using the .Tag Property of each ListViewItem added. The .Tag property is a hidden property which can be used for such cases as this. Each time you add an item to the ListView, you set the .Tag to something. If it's the correct .Tag, then the Timer will subtract time from that item.

See if this helps.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With ListView1 '// setup ListView.
            .Columns.Add("Column 1", 100) : .Columns.Add("Column 2", 100) : .Columns.Add("Column 3", 100) : .Columns.Add("Column 4", 100) : .Columns.Add("Column Time", 100) : .View = View.Details : .FullRowSelect = True
        End With
        With Timer1
            .Interval = 1000
            .Start()
        End With
        Button1.Text = "Add User" : Button2.Text = "Start/Stop"
    End Sub

    Private arTimer() As String = Nothing '// Array to .Split time into hours/seconds.

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items in ListView.
            '///////////////////////////////
            If itm.Tag.ToString = "start countdown" Then
                '////////////////////
                With itm.SubItems(4) '// Get column 5.
                    If .Text.Contains(":") Then '// if it .Contains the ":".
                        arTimer = .Text.Split(":"c) '// .Split it into 2 Arrays.
                        If arTimer(1) = "00" Then '// check Array 2 for "00".
                            If CInt(arTimer(0)) > 0 Then '// if minutes are at "00" and Array 1 is greater than 0, the hours,
                                arTimer(1) = "59" '// set seconds to 59, and
                                arTimer(0) = CStr(CInt(arTimer(0)) - 1) '// subtract hour.
                            End If
                        End If
                        If Not .Text = "0:00" Then '// check if not at "0:00".
                            arTimer(1) = CStr(CInt(arTimer(1)) - 1) '// subtract 1 second from seconds.
                            If arTimer(1).Length = 1 Then arTimer(1) = "0" & arTimer(1) '// add a "0" if seconds are from 0-9.
                            .Text = arTimer(0) & ":" & arTimer(1) '// set .Text back to ListViewItem.
                        End If
                    End If
                End With
            End If
        Next
    End Sub

    '// Add users.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Static iNext As Integer = 1 '// for user #.
        Dim sTemp As String = "user " & iNext.ToString & ",subitem,subitem 2,subitem 3,10:00"
        Dim lvItem As New ListViewItem(sTemp.Split(CChar(",")))
        lvItem.Tag = "stop countdown" '// set to "stop ..." to not automatically start the countdown.
        ListView1.Items.Add(lvItem)
        iNext += 1 '// set # for next user.
        ListView1.Select()
    End Sub

    '// Start/Stop time for each individual user.
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If Not ListView1.SelectedItems.Count = 0 Then '// check if item is selected.
            With ListView1.SelectedItems(0)
                If .Tag.ToString = "start countdown" Then .Tag = "stop countdown" Else .Tag = "start countdown" '// toggle start/stop of countdown.
            End With
            ListView1.Select()
        End If
    End Sub
End Class

im getting error with this line.

If .Tag.ToString = "start countdown" Then .Tag = "stop countdown" Else .Tag = "start countdown" '// toggle start/stop of countdown.

when i click the add user

http://www.daniweb.com/forums/attachment.php?attachmentid=21512&stc=1&d=1309867968


and when a start the time

http://www.daniweb.com/forums/attachment.php?attachmentid=21513&stc=1&d=1309868071

Seems that your error is because you are not setting anything to item.Tag when adding new item to the ListView.

I have added a new If statement in the Timer1_Tick event, lines 18-20 of my previous posted code.

'///////////////////////////////
            If Not itm.Tag Is Nothing Then
                If itm.Tag.ToString = "start countdown" Then
                    '////////////////////

I also added the same If statement for lines 55-57.

With ListView1.SelectedItems(0)
                '// toggle start/stop of countdown.
                If Not .Tag Is Nothing Then If .Tag.ToString = "start countdown" Then .Tag = "stop countdown" Else .Tag = "start countdown"
            End With

This should stop the errors, although your Timer will not subtract time from the items since you need to have the .Tag set to "start countdown".

All you have to do when adding a new item to your ListView, set the .Tag property of it to "start countdown".

Dim lvItem As New ListViewItem(sTemp.Split(CChar(",")))
        '////////////////////////
        lvItem.Tag = "stop countdown" '// set to "stop ..." to not automatically start the countdown.
        '//////////////////////
        ListView1.Items.Add(lvItem)

Seems that your error is because you are not setting anything to item.Tag when adding new item to the ListView.

I have added a new If statement in the Timer1_Tick event, lines 18-20 of my previous posted code.

'///////////////////////////////
            If Not itm.Tag Is Nothing Then
                If itm.Tag.ToString = "start countdown" Then
                    '////////////////////

I also added the same If statement for lines 55-57.

With ListView1.SelectedItems(0)
                '// toggle start/stop of countdown.
                If Not .Tag Is Nothing Then If .Tag.ToString = "start countdown" Then .Tag = "stop countdown" Else .Tag = "start countdown"
            End With

This should stop the errors, although your Timer will not subtract time from the items since you need to have the .Tag set to "start countdown".

All you have to do when adding a new item to your ListView, set the .Tag property of it to "start countdown".

Dim lvItem As New ListViewItem(sTemp.Split(CChar(",")))
        '////////////////////////
        lvItem.Tag = "stop countdown" '// set to "stop ..." to not automatically start the countdown.
        '//////////////////////
        ListView1.Items.Add(lvItem)

still i get some error for this lines sir.

That code works fine here, especially if I add a .Tag to each item in the ListView.

Try it in a new project and if it still errors, post the code here and let us know which lines are throwing what error.

That code works fine here, especially if I add a .Tag to each item in the ListView.

Try it in a new project and if it still errors, post the code here and let us know which lines are throwing what error.

in this line sir, sorry for many questions that i ask because im a newbie in vb programming. hope you understand sir.
can you post the whole code sir.


http://www.daniweb.com/forums/attachment.php?attachmentid=21534&stc=1&d=1309959650

For that sTemp underline error, copy entire code from lines 43 to 49 in my originally posted code. It is showing as an error since you do not have sTemp declared, as "Dim sTemp As String...".

The second one, just by looking at the image, I can say that you are probably missing the End if for that If statement line. Try adding an End If right above the Next line in the Timer1_Tick event and let me know the results.

For that sTemp underline error, copy entire code from lines 43 to 49 in my originally posted code. It is showing as an error since you do not have sTemp declared, as "Dim sTemp As String...".

The second one, just by looking at the image, I can say that you are probably missing the End if for that If statement line. Try adding an End If right above the Next line in the Timer1_Tick event and let me know the results.

they keep on adding subitems and when i start the time of the selected user its not starting, like in the attachment sir.

is there an other way to solvce my problem sir? the 1st code you gave sir was pretty awesome but the problem with that code is starting the time in different users the continues updating the time when other users are selected.

http://www.daniweb.com/forums/attachment.php?attachmentid=21536&stc=1&d=1309961590

Post your code, "sir"!

Post your code, "sir"!

This is the 1st code that you gave. is there other way to starting the time in different users and continues updating the time when other users are selected.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With Timer1
            .Interval = 1000 '// set interval to 1,000, which is 1 second.
            .Start() '// start the Timer.
        End With
    End Sub

    Private arTimer() As String = Nothing

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items in ListView.
            With itm.SubItems(4) '// Get column 5.
                If .Text.Contains(":") Then '// if it .Contains the ":".
                    arTimer = .Text.Split(":"c) '// .Split it into 2 Arrays.
                    If arTimer(1) = "00" Then '// check Array 2 for "00".
                        If CInt(arTimer(0)) > 0 Then '// if minutes are at "00" and Array 1 is greater than 0, the hours,
                            arTimer(1) = "59" '// set seconds to 59, and
                            arTimer(0) = CStr(CInt(arTimer(0)) - 1) '// subtract hour.
                        End If
                    End If
                    If Not .Text = "0:00" Then '// check if not at "0:00".
                        arTimer(1) = CStr(CInt(arTimer(1)) - 1) '// subtract 1 second from seconds.
                        If arTimer(1).Length = 1 Then arTimer(1) = "0" & arTimer(1) '// add a "0" if seconds are from 0-9.
                        .Text = arTimer(0) & ":" & arTimer(1) '// set .Text back to ListViewItem.
                    End If
                End If
            End With
        Next
    End Sub
End Class

Looks like you have not bothered to try and modify the code with the updates I posted in this post.
It should not be too much of a difficulty adding/editing code lines to code already there, even for a vb.noob.
Try updating the code yourself and if still w/out results, post the updated code.

Looks like you have not bothered to try and modify the code with the updates I posted in this post.
It should not be too much of a difficulty adding/editing code lines to code already there, even for a vb.noob.
Try updating the code yourself and if still w/out results, post the updated code.

This my modified code in timer tick, but nothing happens. :(

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items in ListView.
            If itm.Tag = "ON" Then
                With TextBox1  '// Get column 5.
                    If .Text.Contains(":") Then '// if it .Contains the ":".
                        arTimer = .Text.Split(":"c) '// .Split it into 2 Arrays.
                        If arTimer(1) = "00" Then '// check Array 2 for "00".
                            If CInt(arTimer(0)) > 0 Then '// if minutes are at "00" and Array 1 is greater than 0, the hours,
                                arTimer(1) = "59" '// set seconds to 59, and
                                arTimer(0) = CStr(CInt(arTimer(0)) - 1) '// subtract hour.
                            End If
                        End If
                        If Not .Text = "0:00" Then '// check if not at "0:00".
                            arTimer(1) = CStr(CInt(arTimer(1)) - 1) '// subtract 1 second from seconds.
                            If arTimer(1).Length = 1 Then arTimer(1) = "0" & arTimer(1) '// add a "0" if seconds are from 0-9.
                            .Text = arTimer(0) & ":" & arTimer(1) '// set .Text back to ListViewItem.
                        End If
                    End If
                End With
            End If
        Next
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.