|
-
Sep 11th, 2006, 06:01 AM
#1
Thread Starter
Junior Member
How can i use timer?
Hi
If i want to repeat an action every X seconds..how can i do that?
and another question: how can i use the real time to be printed in an output file?
Thanks
-
Sep 11th, 2006, 06:08 AM
#2
PowerPoster
Re: How can i use timer?
First things first: how to use the timer
Timers work in milliseconds...once you have the timer on your form (let's assume you call it timer1) you call it using "timer1.interval = 6000"...this sets it to "fire" after 6 seconds. Now double-click the timer control on your form and put code in for what you want to happen every 6 seconds. If at any time you need to stop the timer, put "timer1.enabled = false" in your code
Second one is a simple pre-defined variable called "time"...if you put "label1 = time" in your code and had label1 on your form, it would have the current time put into it. It's as simple as that to do the same to a textbox or an output to file or printer
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 11th, 2006, 06:28 AM
#3
Re: How can i use timer?
Try this (state on how many seconds you would like to write date and time to file):
VB Code:
Option Explicit
Dim interval As Integer
Dim WithEvents myTimer As Timer
Private Sub Form_Load()
Set myTimer = Me.Controls.Add("VB.Timer", "myTimer")
Do Until IsNumeric(interval) = True And interval > 0
interval = InputBox("State your inteval (in s):", "Interval", "10")
interval = interval * 1000 'multiply to get ms
Loop
myTimer.interval = interval
End Sub
Private Sub myTimer_timer()
Dim ff As Integer
ff = FreeFile
Open "c:\test.txt" For Output As #ff
Print #ff, Date & " - " & Time 'writing to file
Close #ff
End Sub
Last edited by gavio; Sep 11th, 2006 at 06:54 AM.
-
Sep 11th, 2006, 06:35 AM
#4
Thread Starter
Junior Member
Re: How can i use timer?
BIG THANKS smUX
your post was usefull and was rated 
by the way, do you know how can i create a file and write some data into it..
-
Sep 11th, 2006, 06:41 AM
#5
PowerPoster
Re: How can i use timer?
creating a file is simple...what usually isn't simple is when you get into different features, or what you can do...here's a quick and dirty way to write an array to a file
VB Code:
fre = FreeFile 'freefile returns the next available free number
Open "c:\data.txt" For Output As #fre
'output is usually the method you would use, or input for
'getting it back in...there's others, but ignore those for now
For b = 1 To 100
Print #fre, Array(b)
'print #fre is printing to the file. We use multiple numbers
'because you can theoretically print(write) to 100+ files at
'a time...some people read in a file with one and write the
'same with another and make changes to the data as it goes
'through this process :-)
Next b
Close fre
'Got to close the file after writing or it'll never be readable
The green lines are comments and are just there to give you extra information. You don't *have* to use an array or a loop, you can put 1 "print #fre," per variable you wish to write, but most people tend to store the data in an array and write it in one big bunch for ease of use
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 11th, 2006, 06:56 AM
#6
Thread Starter
Junior Member
Re: How can i use timer?
Thank you very much gavio and smUX
but how can i write in the file witout losing the information written in it?
-
Sep 11th, 2006, 07:00 AM
#7
PowerPoster
Re: How can i use timer?
Do you mean an already saved file? If so, change "output" to "append" and it'll append the data to the end of the file
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 11th, 2006, 07:01 AM
#8
Re: How can i use timer?
Open it, read it, add something and write it. For example:
VB Code:
Dim iFF As Integer
Dim sFile As String
Dim sContents As String
iFF=FreeFile
sFile="c:\test.txt"
'read it
Open sFile For Input As #iFF
sContents=Input(LOF(iFF),iFF)
Close #iFF
'write what was before and add something new
Open sFile For Output As #iFF
Print #iFF, sContents
Print #iFF, "something new"
Close #iFF
edit: or like smuX suggested
-
Sep 11th, 2006, 07:05 AM
#9
Re: How can i use timer?
here's another way of writing the array at once:
VB Code:
Open "C:\data.txt" For Output As #fre
Print #fre, Join(MyArray, vbCrLf)
Close #fre
-
Sep 11th, 2006, 07:05 AM
#10
PowerPoster
Re: How can i use timer?
Also, "sContents=Input(LOF(iFF),iFF)" uses a clever trick to take the whole file into a string quickly...although I think it might be missing something (or gavio is using a format I don't know about for input :-))
Another way to do what Gavio did with loading a file quickly is in my sig below :-)
Edit: and I never knew about the join command that Bush suggested there...could be useful later :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 11th, 2006, 07:11 AM
#11
Thread Starter
Junior Member
Re: How can i use timer?
Thanks to all of you guys..you were really helpful and my problems were solved 
i uesd Append that smUX suggested
-
Sep 11th, 2006, 07:12 AM
#12
PowerPoster
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Sep 11th, 2006, 07:14 AM
#13
Re: How can i use timer?
If you got your answer then you can pull down the Thread Tools menu and Mark Thread Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|