PDA

View Full Version : How do you make a VB chronometer/clock?



The_Doctor
02-26-2006, 19:44
You may have read about my VB project that was causing me a lot of problems. I have solved all of my problems except one.

When the user logs in I want a clock to start. When they log out it will stop. This is to record how long they have been working for.

I have no idea how I do this.:help:

Phatose
02-27-2006, 04:06
On login:

Dim Logintime as Date = now();


On logout;
TextBox1.Text = Now().Subtract(Logintime).ToString

The_Doctor
02-27-2006, 11:04
Thanks:2thumbsup: , but that is not quite what I am looking for.

I need a text box or label that says 00:00:00 when the user logs in. Then it acts like a stopwatch, increasing until the user logs out.

Phatose
02-28-2006, 04:27
OK then, here's how you do that.


First, add a timer control to your form. It's in windows forms like the textboxes and whatnot. Presumably you'll want to set the interval to 1000 (since it's in MS), and enabled=false. At the top, in the form itself dim a logintime variable as a date.



In your login code, do this:

logintime = now
timer1.enabled = true

and on logout, presumably
timer1.enabled = false



your timer1 tick event code should be like:

TextBox1.Text = Format(Now.Subtract(logintime).Hours, "00") & ":" & Format(Now.Subtract(logintime).Minutes, "00") & ":" & Format(Now.Subtract(logintime).Seconds, "00")

Just A Girl
02-28-2006, 05:07
Would java be ok?


<head>
<script>
<!--
//
var milisec = 0;
var seconds = 0;

function display(){
if (milisec>=9){
milisec=0
seconds+=1
}
else
milisec+=1
document.counter.ShambleS.value = seconds + "." + milisec;
setTimeout("display()",100);
}

-->
</script>
</head>

<body onload="display();">

<form name="counter">
<input type="text" size="10" name="ShambleS">
</form>

I just noticed...
i think you want it to Log how long people were there.
Not just display it for their benafit...
and you want it in VB.
So i cant help i dont think.

But if the Java script is good enough..

You can add more variables your self..

I.e
var mins = 0;
var hours = 0;

For mins and hours...

then you can add
if (seconds>=60).....
and then the diferent variables there of..

youd also need to change This...

document.counter.ShambleS.value = seconds + "." + milisec;

to something like
document.counter.ShambleS.value = hours + "." + mins + "." + seconds + "." + milisec;

To add the extra decimal points in the clock counter..
"the words are the same as the names you gave your variables"

"note: .counter.ShambleS. are related to the form name and input type name. and can be changed to any thing you like (but they need to be the same name in both)"

But like i said.
i doubt its what you want.

The_Doctor
02-28-2006, 11:37
It sort works. It is counting, only the format does not work.

I had to play around with it a bit.

TextBox1.Text = Format(Now.Subtract(logintime).Hours, "00") & ":" & Format(Now.Subtract(logintime).Minutes, "00") & ":" & Format(Now.Subtract(logintime).Seconds, "00")

First I changed the name of the text box to txtTimeWorked.
The ".Subtract" does not work so replaced them with "-".
Then the ".Hours", ".Minutes" and ".Seconds" caused a "Expected: list seperator or )" error.

Then it looked like this:
Private Sub Timer1_Timer()
txtTimeWorked.Text = Format(Now - (Logintime), Hours"00") & ":" & Format(Now - (Logintime), Minutes"00") & ":" & Format(Now - (Logintime), Seconds"00")
End Sub

It said "Expected: list seperator or )".

Now it looks like this:

Private Sub Timer1_Timer()
txtTimeWorked.Text = Format(Now - (Logintime), Hours("00")) & ":" & Format(Now - (Logintime), Minutes("00")) & ":" & Format(Now - (Logintime), Seconds("00"))
End Sub

The bits in bold are causing problems. It says "sub or function not defined".

Phatose
02-28-2006, 20:12
Are those commas actually commas, or periods like they were in my post?

The_Doctor
02-28-2006, 21:19
Are those commas actually commas, or periods like they were in my post?

They are commas.

I just realised that the commas should after the hours, minutes and seconds, not is front of them.

The periods cause an "Expected: list seperator or )" error. So does hours, minutes and seconds.

The timer code looks like this:

Private Sub Timer1_Timer()

txtTimeWorked.Text = Format(Now - (Logintime)hours, "00") & ":" & Format(Now - (Logintime)minutes, "00") & ":" & Format(Now - (Logintime)seconds, "00")

End Sub

The_Doctor
03-03-2006, 21:48
I continued messing around with the code and got it working. It was a lot shorter than what was posted.

So, a big thank you to Phatose.:balloon2:


Would java be ok?

Maybe next year.:2thumbsup:

Phatose
03-04-2006, 07:47
Glad to hear you got it working.