14 Replies - 3587 Views - Last Post: 28 March 2013 - 02:11 PM Rate Topic: ***** 1 Votes

#1 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Count Time From Button Click

Posted 28 March 2013 - 11:58 AM

Hello There,
i want to count the time from the user enter on button, and store it in variable, just like this ::
Dim strResult As String = String.Empty
        strResult &= (Environment.TickCount \ 86400000).ToString + " days, "
        strResult &= (Environment.TickCount / 3600000 Mod 24).ToString("n0") + " hours, "
        strResult &= (Environment.TickCount / 120000 Mod 60).ToString("n0") + " minutes, "
        strResult &= (Environment.TickCount / 1000 Mod 60).ToString("n0") + " seconds."
        MsgBox(strResult)

This is StartUp Time, Now am trying to Get Button Click Time Like Above Code,,
i am using this code, but its not work, so help me to solve this..
Code ::
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        timer1.start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblhrs.Text = Now.Hour
        lblmin.Text = Now.Minute
        lblsec.Text = Now.Second
        Dim a As String = (Val(lblhrs.Text) / 3600000 Mod 24).ToString("n0") + " Hours, "
        Dim b As String = (Val(lblmin.Text) / 120000 Mod 60).ToString("n0") + " Minutes, "
        Dim c As String = (Val(lblmin.Text) / 1000 Mod 60).ToString("n0") + " Seconds, "
        lblmain.Text = a + b + c
    End Sub


Thnks In Adv..
@Nilesh

Is This A Good Question/Topic? 0
  • +

Replies To: Count Time From Button Click

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Count Time From Button Click

Posted 28 March 2013 - 12:02 PM

I'm thinking you're wanting to use the StopWatch instead of the timer.
Was This Post Helpful? 1
  • +
  • -

#3 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Count Time From Button Click

Posted 28 March 2013 - 12:09 PM

View PostCharlieMay, on 28 March 2013 - 12:02 PM, said:

I'm thinking you're wanting to use the StopWatch instead of the timer.

no, i want to count the time when at clicked on that button..??
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 329
  • View blog
  • Posts: 996
  • Joined: 01-February 13

Re: Count Time From Button Click

Posted 28 March 2013 - 12:09 PM

Hi,
Are you trying to get a running total of the time sense the button was pressed or just the time that it was pressed?
Was This Post Helpful? 1
  • +
  • -

#5 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Count Time From Button Click

Posted 28 March 2013 - 12:12 PM

View PostIronRazer, on 28 March 2013 - 12:09 PM, said:

Hi,
Are you trying to get a running total of the time sense the button was pressed or just the time that it was pressed?

yeah...is it possible??? yes then help
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 329
  • View blog
  • Posts: 996
  • Joined: 01-February 13

Re: Count Time From Button Click

Posted 28 March 2013 - 12:16 PM

Hey,
Here is a little code to get the time that the button was pressed. If you want the date on it to then remove the line that gets the substring.
Public Class Form1
    Dim btntime As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        btntime = My.Computer.Clock.LocalTime.ToString
        btntime = btntime.Substring(btntime.IndexOf(" "))
        MessageBox.Show(btntime)
    End Sub
End Class


:bigsmile:
Was This Post Helpful? 1
  • +
  • -

#7 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Count Time From Button Click

Posted 28 March 2013 - 12:27 PM

View PostIronRazer, on 28 March 2013 - 12:16 PM, said:

Hey,
Here is a little code to get the time that the button was pressed. If you want the date on it to then remove the line that gets the substring.
Public Class Form1
    Dim btntime As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        btntime = My.Computer.Clock.LocalTime.ToString
        btntime = btntime.Substring(btntime.IndexOf(" "))
        MessageBox.Show(btntime)
    End Sub
End Class


:bigsmile:/>

:(..
its not done,
i want to count the time when at clicked on that button..??
if i have clicked on button 2 minutes ago then its shows like this.. 0 hours:2 minutes:00 Seconds

is it?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,527
  • Joined: 12-December 12

Re: Count Time From Button Click

Posted 28 March 2013 - 12:41 PM

View PostIronRazer, on 28 March 2013 - 07:16 PM, said:

Hey,
Here is a little code to get the time that the button was pressed. If you want the date on it to then remove the line that gets the substring.
Public Class Form1
    Dim btntime As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        btntime = My.Computer.Clock.LocalTime.ToString
        btntime = btntime.Substring(btntime.IndexOf(" "))
        MessageBox.Show(btntime)
    End Sub
End Class


:bigsmile:

@IronRazer - there's always more than one way :)

My.Computer.Clock.LocalTime.ToLongTimeString    '19:39:05

Was This Post Helpful? 2
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Count Time From Button Click

Posted 28 March 2013 - 12:43 PM

I guess you could create a variable to hold the time it was pressed (at the forms scope)

and then in a timer event, use a TimeSpan to calculate out the span in hours, minutes, seconds and use those results to build the string to display in the button with each timer tick.

MSDN Timespan

This post has been edited by CharlieMay: 28 March 2013 - 12:44 PM

Was This Post Helpful? 1
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Count Time From Button Click

Posted 28 March 2013 - 12:43 PM

Then that's the time since the Button was clicked.

So, as you were advised before, the StopWatch is the best way.

Dim sw As New Stopwatch

sw.Start
sw.Stop
sw.Elapsed
Was This Post Helpful? 2
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Count Time From Button Click

Posted 28 March 2013 - 12:50 PM

Actually now that I understand what he's doing, a timespan would be an easy solution.

click button and store datetime.now in a variable and start the timer

In the timer
create a timespan
set the timespan = datetime.now - dateclicked
set button text to a string format of ts.hours, ts.minutes, ts.seconds.

This would allow the button to update with each second after it was clicked.

This post has been edited by CharlieMay: 28 March 2013 - 01:09 PM

Was This Post Helpful? 2
  • +
  • -

#12 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Count Time From Button Click

Posted 28 March 2013 - 01:06 PM

View PostCharlieMay, on 28 March 2013 - 12:50 PM, said:

Actually lar3ry, after seeing what he's doing, a timespan would be an easy solution.

click button and store datetime.now in a variable and start the timer

In the timer
create a timespan
set the timespan = datetime.now - dateclicked
set button text to a string format of ts.hours, ts.minutes, ts.seconds.

This would allow the button to update with each second after it was clicked.

can you explain it?? m confusd... :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Count Time From Button Click

Posted 28 March 2013 - 01:11 PM

did you read the link on how the timespan works?

It takes two datetimes and creates a span of time between the two. Then the structure allows you to see the difference by displaying the hours, minutes and seconds (and even milliseconds) properties.
Was This Post Helpful? 1
  • +
  • -

#14 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 605
  • Joined: 01-October 08

Re: Count Time From Button Click

Posted 28 March 2013 - 01:16 PM

Something like this will show the time between successive button clicks.


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Static stpw As New Stopwatch
        stpw.Stop()
        Debug.WriteLine(stpw.Elapsed.ToString)
        stpw.Reset()
        stpw.Start()
    End Sub


Was This Post Helpful? 0
  • +
  • -

#15 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 329
  • View blog
  • Posts: 996
  • Joined: 01-February 13

Re: Count Time From Button Click

Posted 28 March 2013 - 02:11 PM

Hey,
I just wanted to let you know that you could also use the TimeSpan to calculate your computers boot time also. It would be a little shorter i think. :bigsmile:
Public Class Form1
    Dim BootUpTime As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tm As Integer = Environment.TickCount
        Dim ts As TimeSpan = TimeSpan.FromMilliseconds(tm)
        BootUpTime = "Days " & ts.Days & "  Hours " & ts.Hours & "  Minutes " & ts.Minutes & "  Seconds " & ts.Seconds
        MessageBox.Show(BootUpTime)
    End Sub
End Class


:bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1