Question How can I use timer in a subitem(column) of listview for my time consumed column?

The_Kid

Member
Joined
Sep 17, 2012
Messages
14
Programming Experience
3-5
I have this column named Time Started ( this data comes from different client) and the other is Time Consumed column. I am the server and I want to animate all the time in Time consumed column by subtracting current time i have. How can I do that? moving timer for subitem(3). Is it much better that it will be based on my pc or always fetch the time consumed/sec on clients computer to the server?
 
People often misunderstand what a Timer is for. It is not like a stopwatch, i.e. it does not measure elapsed time. There is the Stopwatch class for that. The Timer class is like an alarm clock, i.e. it will notify you after a specific period of time has passed. So, in your case, the Timer can notify you repeatedly at regular intervals, e.g. every second, and thereby give you an opportunity to update your UI. As for measuring the time though, you could just subtract a couple of DateTimes but I'd suggest using a Stopwatch. It can be started, stopped, paused and reset, just like a real stopwatch. At any point, you can get a TimeSpan from its Elapsed property that gives you the elapsed time.
 
Never heard of a stopwatch :) . Ill try to check it out but my main problem is that even though i know how to subtract time span , I wonder how to apply it in a whole list view column(e.g.subitem(3))?
 
Last edited:
View attachment LikeStopwatches.zipAhh, how i detest listboxes that are used like grids. If you want a grid layout, use a grid instead.

Add a dataset to your project. i left mine called dataset1; name yours something mkore suitable
Use the dataset designer to add a datatable, i left mine called datatable1; name yours something more suitable
Add 3 columns to the table: task as string, timestarted as date, timeconsumed as timespan
Put a timer on your form, set its interval to 1000 (milliseconds = 1 second), make it enabled
Switch to form design view
Show the data sources window
Drag the table to the form
Switch to code view
In the constructor (New() ) for the form, put the following code:

VB.NET:
 Me.DataSet1.DataTable1.AddDataTable1Row("Make breakfast", DateTime.Now.AddHours(-9.5123), TimeSpan.FromDays(0))
    Me.DataSet1.DataTable1.AddDataTable1Row("Make lunch", DateTime.Now.AddHours(-6.12234), TimeSpan.FromDays(0))
    Me.DataSet1.DataTable1.AddDataTable1Row("Make dinner", DateTime.Now.AddHours(-3.213423), TimeSpan.FromDays(0))
    Me.DataSet1.DataTable1.AddDataTable1Row("Make supper", DateTime.Now, TimeSpan.FromDays(0))

Switch to design view, double click the timer. Put the following code in the timer tick event

VB.NET:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    For Each ro As DataSet1.DataTable1Row In DataSet1.DataTable1
      ro.TimeConsumed = DateTime.Now - ro.TimeStarted
    Next
  End Sub

example project attached, but it was created in VS2010 which you may not have, so you'd likely need to add the individual files to your project if the project file wont open
View attachment LikeStopwatches.zip
 
Hi,

Here is another example for you using a ListView control. Add a listview control and timer control to a form. Again set the timer to enabled with an interval value of 1000 (1 Second), Set the ListView View Property to Details and then add the following code to the form.

VB.NET:
Public Class Form1
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Timer1_Tick(sender, e)
  End Sub
 
  Private Sub AddListViewHeader()
    Dim LVHeader() As ColumnHeader = {New ColumnHeader With {.Text = "Start Time", .Width = ListView1.Width * 0.33, .TextAlign = HorizontalAlignment.Left},
                                      New ColumnHeader With {.Text = "Current Time", .Width = ListView1.Width * 0.33, .TextAlign = HorizontalAlignment.Center},
                                      New ColumnHeader With {.Text = "Elapsed Time", .Width = -2, .TextAlign = HorizontalAlignment.Center}}
    ListView1.Columns.Clear()
    ListView1.Columns.AddRange(LVHeader)
  End Sub
 
  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    Static Counter As Integer
 
    For Each curLVI As ListViewItem In ListView1.Items
      With curLVI
        .SubItems(1).Text = Now.ToLongTimeString
        .SubItems(2).Text = (DateTime.Parse(.SubItems(1).Text) - DateTime.Parse(.Text)).ToString
      End With
    Next
 
    Counter += 1
    If Counter <= 20 Then
      Dim LVI As New ListViewItem
      With LVI
        .Text = Now.ToLongTimeString
        .SubItems.Add(Now.ToLongTimeString)
        .SubItems.Add((DateTime.Parse(.SubItems(1).Text) - DateTime.Parse(.Text)).ToString)
      End With
      ListView1.Items.Add(LVI)
      AddListViewHeader()
    End If
  End Sub
End Class
Hope it helps and Good Luck.

Ian
 
Last edited:
Thanks for the reply cjard and IanRyder.

@cjard
Sorry man but I am practicing listview this time. ;)

@IanRyder
How can I do it without AddListview header? Just plane getting subitem..
 
Hi,

With the ListView control you have to create a header to create the columns in the Details view of the ListView control. The only way to effectively remove the header (with little difficulty) is to set each columns text to "". You will still have a header which creates the columns in the ListView control but the header will just be empty.

To truly delete the header in the control you have to delve into windows API and messaging. Have a look here for more information :- http://stackoverflow.com/questions/1814918/vb-listview-change-header-height

Does that answer your question?

Cheers,

Ian
 
Thanks again for your quick reply.
Sorry I mistyped it.
I actually asked if I created the Listview manually including its column header, how can I do that without add code that you shown in your previous post.
 
Hi,

Just thought of this, but as a total CHEAT, create a label with some fancy introduction text such as "My Fancy Timer ListView Control" and drop it onto the header of the ListView thereby hiding the header? You need to ensure that the label is brought to the front with regards to the order of the controls.

Good Luck.

Ian
 
Hi,

Sorry, but scratching my head now? If you are saying that you have already created the headers through the properties tab in design time then all you need to do is comment out the call to AddListViewHeader to keep the header you have created?

Am I understanding you correctly?

Ian.
 
Yes. I actually have created it through properties tab. All I want now is the code of the timer knowing that there is already a data in the subitem(1). I do not want to use

Private Sub AddListViewHeader() because I already created mine manually? Sorry if made your head scratch :'(
VB.NET:
Public Class Form1   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load     
Timer1_Tick(sender, e)  
 End Sub       

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick    
 Static Counter As Integer  
     For Each curLVI As ListViewItem In ListView1.Items     
  With curLVI      
   .SubItems(1).Text = Now.ToLongTimeString      
   .SubItems(2).Text = (DateTime.Parse(.SubItems(1).Text) - DateTime.Parse(.Text)).ToString     
  End With   
  Next      
End Sub
End Class
 
Hi The_Kid,

You have not only made me scratch my head but now I am totally confused? You made the comment:-

Yes. I actually have created it through properties tab. All I want now is the code of the timer knowing that there is already a data in the subitem(1). I do not want to use Private Sub AddListViewHeader() because I already created mine manually?

But that deos not tell me anything about what you need from me now to help you further? I am guessing that it has something to do with the comment:-

All I want now is the code of the timer knowing that there is already a data in the subitem(1)

You already have an example of how the timer works and how to edit listview subitems so what is it that you do not understand of the example code that I have already given you?

Ian.
 
Hi,

OK, This makes a bit more sense. The question is why did it not work? You have to let us know these details to help you better.

Do you have 3 columns in your ListView control to represent .text (being column 1, .SubItems.Text(0)), .SubItems.Text (1) (being column 2) and .SubItems.Text (2) (being column 3)? If not, then you need to add the additional columns.

You then need to add the start time to .Text
You then need to add the current time to . SubItems.Text (1)
You then need to add the Elapsed time calculation to . SubItems.Text (2)

That should do it for you??? Remember that you need to adapt the sample given above to your specific situation!

If not please post back with your specific errors so that I can help you better.

Good Luck,

Ian
 
Back
Top