Results 1 to 7 of 7

Thread: Working with time

  1. #1

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Working with time

    Hello,

    What I am trying to do is find a method to find out how often something happends within a time frame. What I have is a TotalTime (total spent on form). And a CompleteButton. What I want to figure out is how many times CompleteButton is clicked in an hour.

    Thanks for any advice...

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Working with time

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim startTime As New TimeSpan(16, 0, 0) '4 o'clock
    4.     Dim endTime As New TimeSpan(17, 0, 0) '5 o'clock
    5.  
    6.     Dim clicks As Integer = 0
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         If Now.TimeOfDay >= startTime And Now.TimeOfDay <= endTime Then
    10.             clicks += 1
    11.             Label1.Text = clicks.ToString
    12.         End If
    13.     End Sub
    14.  
    15. End Class

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Working with time

    Add a global variable

    Dim Count As Int16 = 0

    Take the time when the application starts (use the form load event)

    Dim Start as DateTime= Now

    Add one hour for Stop

    In the button click event

    If Stop > Now increment Count and display the running total


    Alternatively keep the count running, take the time at the end of an arbitrary period and divide count by the hours expired.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Working with time

    Quote Originally Posted by .paul. View Post
    try this:
    Yeah, something like that. (How does he do that so quick?)

  5. #5

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with time

    Thank you both very much.

    I think I might have not properly said what I was trying to do because you both answered me perfect.

    I guess my goal is to track an employee's production. So the theory I have came up with is that Production = Total Time / Clicks ...... and then all that broken down to hours... and the program has many forms... so i want to track how long they are on say form1 and how many times they click it within the total time frame...if that makes sense

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Working with time

    ok. that's slightly more complicated. i used a button, a numericupdown, + a label.
    the number of clicks is stored per hour, starting when you load the app. if there is more than 1 hour, you can select a different hour with the numericupdown.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim startTime As TimeSpan
    4.  
    5.     Dim clicks As New List(Of Integer)
    6.     Dim listIndex As Integer = 0
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         Dim endTime As TimeSpan = Now.TimeOfDay
    10.         Dim elapsed As TimeSpan = endTime - startTime
    11.         If elapsed.TotalMinutes >= 60 Then
    12.             clicks.Add(0)
    13.             startTime = startTime.Add(New TimeSpan(1, 0, 0))
    14.             listIndex += 1
    15.             NumericUpDown1.Maximum += 1
    16.             NumericUpDown1.Value = NumericUpDown1.Maximum
    17.         End If
    18.         clicks(listIndex) += 1
    19.         Label1.Text = String.Format("{0} hour clicks = {1}", getNumberWithSuffix(CInt(NumericUpDown1.Value) + 1), clicks(CInt(NumericUpDown1.Value)))
    20.     End Sub
    21.  
    22.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    23.         startTime = Now.TimeOfDay
    24.         NumericUpDown1.Maximum = 0
    25.         clicks.Add(0)
    26.     End Sub
    27.  
    28.     Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    29.         Label1.Text = String.Format("{0} hour clicks = {1}", getNumberWithSuffix(CInt(NumericUpDown1.Value) + 1), clicks(CInt(NumericUpDown1.Value)))
    30.     End Sub
    31.  
    32.     Private Function getNumberWithSuffix(ByVal x As Integer) As String
    33.         If x > 100 Then Return "Unsupported"
    34.         Select Case x
    35.             Case 1, 21, 31, 41, 51, 61, 71, 81, 91
    36.                 Return x.ToString & "st"
    37.             Case 2, 22, 32, 42, 52, 62, 72, 82, 92
    38.                 Return x.ToString & "nd"
    39.             Case 3, 23, 33, 43, 53, 63, 73, 83, 93
    40.                 Return x.ToString & "rd"
    41.             Case Else
    42.                 Return x.ToString & "th"
    43.         End Select
    44.     End Function
    45.  
    46. End Class

  7. #7

    Thread Starter
    Member jbailey01's Avatar
    Join Date
    Jul 2012
    Posts
    45

    Re: Working with time

    Thank You .paul.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width