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?
 
After few minutes of looking I figured it out


VB.NET:
Public Class Form1
    Dim timenow As Date
    Dim timelapse As TimeSpan


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        timelapse = TimeOfDay - timenow
        For Each curLVI As ListViewItem In ListView1.Items
            With curLVI
                .SubItems(2).Text = (timelapse).ToString
            End With
        Next
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Enabled = True


    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim arr(2) As String
        timenow = TimeOfDay
        arr(0) = "Joshua"
        arr(1) = Convert.ToString(TimeOfDay)
        ListView1.Items.Add(New ListViewItem(arr))
        arr(0) = "craig"
        arr(1) = Convert.ToString(TimeOfDay)
        ListView1.Items.Add(New ListViewItem(arr))
        arr(0) = "David"
        arr(1) = Convert.ToString(TimeOfDay)
        ListView1.Items.Add(New ListViewItem(arr))
    End Sub
End Class

but applying it in my program was the question.
This for receiving message where the event will occur when to start the timer
Sorry i cannot analyze this now because I am going to have my quiz so maybe you can help me about this one.

Timer
VB.NET:
   Private Sub timeconsume_Tick(sender As Object, e As EventArgs) Handles timeconsume.Tick
        For Each curLVI As ListViewItem In clientView.Items
            With curLVI
                timelapse = Date.Parse(curLVI.SubItems(2).Text) - TimeOfDay
                .SubItems(3).Text = (timelapse).ToString
            End With
        Next
    End Sub

VB.NET:
Private Sub OnIncomingMessage(ByVal Args As UNOLibs.Net.ServerClass.InMessEvArgs) Handles Server.IncomingMessage
        'sender IP
        Try
            Dim sip As String = Args.senderIP
            Dim DATA As String = Args.message
            Dim itemFound As ListViewItem = clientView.FindItemWithText(sip)
            If (itemFound IsNot Nothing) Then
                MsgBox("already in " & itemFound.ToString)


                If DATA.Contains("login") Then

                    Dim loginfo() As String = DATA.Split(".")
                    MsgBox(loginfo(1) & loginfo(2) & loginfo(0))
                    MsgBox(itemFound.Index.ToString)
                    itemFound.SubItems(2).Text = loginfo(1)
                    itemFound.SubItems(1).Text = loginfo(2)
                End If
            Else
                MsgBox("message receive" & DATA)
                MsgBox("data" + sip)
                timeconsume.Enabled = True
                Dim ipDetail(7) As String
                ipDetail(0) = DATA
                ipDetail(1) = "Test"
                ipDetail(2) = "1"
                ipDetail(4) = "3"
                ipDetail(5) = "4"
                ipDetail(6) = sip
                ipDetail(7) = "5"
// ipdetail(3) = is time consume column
                clientView.Items.Add(New ListViewItem(ipDetail))
            End If
        
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
       
End Sub
 
Hi The_Kid,

I have figured out what you are trying to do now but to give you a clear working example of what you need to do then please can you send me a TRUE DATA example of what you receive from the incoming server message in the form of "xxx.yyy.zzz.etc". Can you also send me what each element in your ipDetail array from 0 to 7 represents. i.e. NOT sample data such as Data,Test,1, 3, but ipDetail(0)="IP Address", ipDetail(1)="Login Name" etc....

If you can provide these two things then I am sure I can give you an example of exactly what you are looking for.

Cheers,

Ian
 
If the login was not found in the listview then
note: those other data where not yet implemented as I am not yet into the database now and those msgbox are just my guide if the data really is in there for easy debugging.
Disregard the other details as they are just my guide for putting the right subitem
VB.NET:
ipDetail(0) = PCNAME
ipDetail(6) = IP address
the trim part is where Ill update that listview if it saw the same ip in the clientview as listview
VB.NET:
loginfo(0)=login
loginfo(1)= Name of User
loginfo(2)= Time started

for better representation here is the listview
sample.png
 
Hi The_Kid,

Please find below an example of what should do the trick for you. To test, create a new form, add a listview called clientView, add a timer called timeconsume and 6 buttons using their default naming convention. Then replace the forms code with the whole of the code below and run.

Each of the buttons are simulating a message coming in from your server by using my own class object called SampleInCommingMessageFromServer which you can see at the bottom of the code.

You can see that if the IP address is not recognised then the message is added to the listview otherwise the listview is updated with the message details from the server. When the first message is received the timer is then activated to keep track of he time consumed.

Again, I have made a few assumptions as to the structure of the message from the server but you should be able to quickly compare the real message to my own to see if any changes need to be made to the structure.

To run in your real life application all you need to do is copy the code from the OnIncomingMessage subroutine to your own and the code of the timer control to your own.

That should be it?

Good Luck,

Ian

VB.NET:
Public Class Form1
  Private NewServerMessage As New SampleInCommingMessageFromServer
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    clientView.Width = 1000
    Me.Width = clientView.Width + 40
    AddListViewHeader()
  End Sub
 
  Private Sub AddListViewHeader()
    Dim LVHeader() As ColumnHeader = {New ColumnHeader With {.Text = "#", .Width = 50, .TextAlign = HorizontalAlignment.Left},
                                      New ColumnHeader With {.Text = "Name", .Width = 200, .TextAlign = HorizontalAlignment.Left},
                                      New ColumnHeader With {.Text = "Time Started", .Width = 130, .TextAlign = HorizontalAlignment.Center},
                                      New ColumnHeader With {.Text = "Time Consumed", .Width = 100, .TextAlign = HorizontalAlignment.Center},
                                      New ColumnHeader With {.Text = "IP Address", .Width = 150, .TextAlign = HorizontalAlignment.Left},
                                      New ColumnHeader With {.Text = "Location", .Width = 200, .TextAlign = HorizontalAlignment.Left},
                                      New ColumnHeader With {.Text = "Card #", .Width = -2, .TextAlign = HorizontalAlignment.Left}}
    clientView.Columns.Clear()
    clientView.Columns.AddRange(LVHeader)
  End Sub
 
  'Private Sub OnIncomingMessage(ByVal Args As UNOLibs.Net.ServerClass.InMessEvArgs) Handles Server.IncomingMessage
  Private Sub OnIncomingMessage(ByVal Args As SampleInCommingMessageFromServer)
    'Assumed data structure from the incomming server message
    '"Login.The_Kid.21/09/12 05:34:25.Baltimore.12345678910"
 
    'clientView ListView Structure
    '.text= "ListView Entry Number"
    '.subItems(1).Text= "PC/User Name"
    '.subItems(2).Text= "Time Started"
    '.subItems(3).Text= "Time Consumed"
    '.subItems(4).Text= "IP Address"
    '.subItems(5).Text= "Location"
    '.subItems(6).Text= "Card Number"
 
    'LogInfo() Array Structure after the split command on DATA variable
    'LogInfo(0)="Login"
    'LogInfo(1)="Name of User"
    'LogInfo(2)="Time Started"
    'LogInfo(3)="Location"
    'LogInfo(4)="Card Number"
 
    'this variable keeps track of the number of NEW ENTRY messages being received from the server
    Static NewMessageCount As Integer
 
    Try
      Dim SIP As String = Args.senderIP
      Dim DATA As String = Args.message
      Dim loginfo() As String = DATA.Split(".")
      Dim itemFound As ListViewItem = clientView.FindItemWithText(SIP)
 
      If (itemFound IsNot Nothing) Then
        If DATA.ToLower.Contains("login") Then
          With itemFound
            .SubItems(1).Text = loginfo(1)
            .SubItems(2).Text = loginfo(2)
            .SubItems(3).Text = (DateTime.Parse(Now()) - DateTime.Parse(loginfo(2))).ToString
          End With
        End If
      Else
        NewMessageCount += 1
        Dim NEWMessage As New ListViewItem
        With NEWMessage
          .Text = NewMessageCount.ToString
          .SubItems.Add(loginfo(1))
          .SubItems.Add(loginfo(2))
          .SubItems.Add((DateTime.Parse(Now()) - DateTime.Parse(loginfo(2))).ToString)
          .SubItems.Add(SIP)
          .SubItems.Add(loginfo(3))
          .SubItems.Add(loginfo(4))
        End With
        clientView.Items.Add(NEWMessage)
 
        'Enable the timer now that at least one New Message has been received
        If Not timeconsume.Enabled Then
          With timeconsume
            .Interval = 1000
            .Enabled = True
          End With
        End If
      End If
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try
  End Sub
 
  Private Sub timeconsume_Tick(sender As System.Object, e As System.EventArgs) Handles timeconsume.Tick
    For Each curLVI As ListViewItem In clientView.Items
      With curLVI
        .SubItems(3).Text = (DateTime.Parse(Now()) - DateTime.Parse(.SubItems(2).Text)).ToString
      End With
    Next
  End Sub
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    NewServerMessage.senderIP = "192.168.0.1"
    NewServerMessage.message = "Login.The_Kid.21/09/12 05:34:25.Baltimore.12345678910"
    Call OnIncomingMessage(NewServerMessage)
  End Sub
 
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    NewServerMessage.senderIP = "192.168.1.23"
    NewServerMessage.message = "Login.IanPC.21/09/12 06:18:00.Healing.12345678910222"
    Call OnIncomingMessage(NewServerMessage)
  End Sub
 
  Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    NewServerMessage.senderIP = "192.168.12.189"
    NewServerMessage.message = "Login.Billy.21/09/12 06:19:00.Atlanta.99999999999999"
    Call OnIncomingMessage(NewServerMessage)
  End Sub
 
  Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
    NewServerMessage.senderIP = "192.168.12.189"
    NewServerMessage.message = "Login.Jimmy.21/09/12 06:20:00.Atlanta.99999999999999"
    Call OnIncomingMessage(NewServerMessage)
  End Sub
 
  Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
    NewServerMessage.senderIP = "192.168.1.23"
    NewServerMessage.message = "Login.JohnPC.21/09/12 06:21:30.Healing.12345678910222"
    Call OnIncomingMessage(NewServerMessage)
  End Sub
 
  Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    NewServerMessage.senderIP = "192.168.0.1"
    NewServerMessage.message = "Login.Helen.21/09/12 06:21:25.Baltimore.12345678910"
    Call OnIncomingMessage(NewServerMessage)
  End Sub
End Class
 
Friend Class SampleInCommingMessageFromServer
  Public Property senderIP As String
  Public Property message As String
End Class
 
Error encountered:
String was not recognized a valid date time.

That error occur to me long ago that is why i change it to timespan=timenow-clienttime
 
Hi The_Kid,

Sorry, but I cannot help you here! Where was the error in the code?

Did you create a new form and run the sample code as is? Did that work?
Have you immediately modified the code to implement in your application and is this where you got the error?

Please be specific my friend.

Cheers,

Ian
 
Hi,

That does not help at all.

Where did you copy and paste the code?
What was the error you got?
Where in the code did you get the error?

It's not very often I give up but if you do not start helping me to help you then I am going to stop trying to help you since I am not a mind reader?

Ian.
 
Hi Ian.
Pure copy paste in a new form named form1 just like what you instructed.
Sorry man but I told you the error was
VB.NET:
String was not recognized a valid date time
and the error will occur if I click any of your 6 button.
 
Last Chance,

I do not need to know what your own interpretation of the error is but what I need to know is the following:-

1) In the example that I gave, what works and what does not work?

1) When the program ends unexpectedly, which I assume it does, what is the specific error that is reported?

2) Where in the code does this occur?

If you can give me a specific answer to these three questions I will try to continue to help otherwise that's me done.

Be specific since this is key to solving your problem.

Ian
 
What is wrong with my previous answer?

1. No error will occur during coding therefore all your code is correct according to my IDE (VS2012). The error will occur during runtime and the thing that will not work is the subtraction of your time.

1. Just as I said it will occur after clicking the buttons and the error is exactly what I posted in my previous post.
VB.NET:
String was not recognized a valid dateTime
2. To make it easier for you to understand Ill show you the line but take note that
my IDE did not exactly tell or highlight this line though
this one obviously is where the error will occur.
VB.NET:
.SubItems(3).Text = (DateTime.Parse(Now()) - DateTime.Parse(loginfo(2))).ToString
 
The format of his sample date strings ("21/09/12 05:34:25") is not recognized by your computer (different culture).
 
Thanks JohnH,

I agree that your comment is the most likely (guaranteed) answer and I have to admit that I should have considered culture info to accommodate non-UK format in the first place.

The_Kid,

You need to change the order of the day and month values in each sample server message string in each button. i.e:-

"Login.IanPC.21/09/12 06:18:00.Healing.12345678910222" needs to become:-
"Login.IanPC.09/21/12 06:18:00.Healing.12345678910222"

Does that now sort the problem?

Cheers,

Ian
 
Yes its working now. Thanks really a lot but my problem now after I applied it in my system, my timer wont run?
I put msgbox("work now!") after consumedtime.enabled = true
to see if it reach there and it did but the problem is ,after that it wont prompt the msgbox inside the timer msgbox("Consumed Time") which means it did not activate my timer.
but if I will put msgbox again after consumedtime.enabled = true
(msgbox("jump"). It will run the timer but will just stop if I will press ok in msgbox("jump")
:( so weird.
VB.NET:
 Private Sub OnIncomingMessage(ByVal Args As UNOLibs.Net.ServerClass.InMessEvArgs) Handles Server.IncomingMessage
        'sender IP
        Try
            Dim sip As String = Args.senderIP
            Dim DATA As String = Args.message
            Dim itemFound As ListViewItem = clientView.FindItemWithText(sip)
            If (itemFound IsNot Nothing) Then
                If DATA.Contains("login") Then
                    Dim loginfo() As String = DATA.Split(",")
                    With itemFound
                        .SubItems(2).Text = loginfo(1)
                        .SubItems(1).Text = loginfo(2)
                        .SubItems(3).Text = (DateTime.Parse(TimeOfDay()) - DateTime.Parse(loginfo(1))).ToString
                    End With
                    onlineCount = onlineCount + 1
                ElseIf DATA = "disconnect" Then
                    itemFound.SubItems(2).Text = String.Empty
                    itemFound.SubItems(1).Text = String.Empty
                    itemFound.SubItems(3).Text = String.Empty
                    itemFound.SubItems(5).Text = String.Empty
                    onlineCount = onlineCount - 1
                ElseIf DATA = "Exit" Then
                    clientView.Items.Remove(itemFound)
                    connectedCount = connectedCount - 1
                End If
            Else
                Dim ipDetail(7) As String
                ipDetail(0) = DATA
                ipDetail(2) = "1"
                ipDetail(5) = "4"
                ipDetail(4) = sip
                ipDetail(7) = "5"
                clientView.Items.Add(New ListViewItem(ipDetail))
                connectedCount = connectedCount + 1
            End If
            If onlineCount > 0 Then
                MsgBox("WORK NOW!!")
                consumedTime.Enabled = True
                MsgBox("Jump")
            ElseIf onlineCount <= 0 Then
                consumedTime.Enabled = False
            End If
            lblOnline.Text = "Online: " & onlineCount
            lblConnected.Text = "Connected: " & connectedCount
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Timer
Properties
Enabled : False
GenerateMember : True
Interval : 1000
Modifiers : Friend

VB.NET:
Private Sub consumedTime_Tick(sender As Object, e As EventArgs) Handles consumedTime.Tick
        MsgBox("Consumed Time")
        For Each curLVI As ListViewItem In clientView.Items
            With curLVI
                .SubItems(3).Text = (DateTime.Parse(Now()) - DateTime.Parse(.SubItems(2).Text)).ToString
            End With
        Next
    End Sub
 
Last edited:
Back
Top