Results 1 to 13 of 13

Thread: How can i use timer?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    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

  2. #2
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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:
    1. Option Explicit
    2.     Dim interval As Integer
    3.     Dim WithEvents myTimer As Timer
    4.    
    5. Private Sub Form_Load()
    6.     Set myTimer = Me.Controls.Add("VB.Timer", "myTimer")
    7.    
    8.         Do Until IsNumeric(interval) = True And interval > 0
    9.             interval = InputBox("State your inteval (in s):", "Interval", "10")
    10.             interval = interval * 1000 'multiply to get ms
    11.         Loop
    12.        
    13.             myTimer.interval = interval
    14. End Sub
    15.  
    16. Private Sub myTimer_timer()
    17.     Dim ff As Integer
    18.    
    19.         ff = FreeFile
    20.        
    21.             Open "c:\test.txt" For Output As #ff
    22.                 Print #ff, Date & " - " & Time 'writing to file
    23.             Close #ff
    24. End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    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..

  5. #5
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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:
    1. fre = FreeFile 'freefile returns the next available free number
    2. Open "c:\data.txt" For Output As #fre
    3. 'output is usually the method you would use, or input for
    4. 'getting it back in...there's others, but ignore those for now
    5. For b = 1 To 100
    6. Print #fre, Array(b)
    7. 'print #fre is printing to the file.  We use multiple numbers
    8. 'because you can theoretically print(write) to 100+ files at
    9. 'a time...some people read in a file with one and write the
    10. 'same with another and make changes to the data as it goes
    11. 'through this process :-)
    12. Next b
    13. Close fre
    14. '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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    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?

  7. #7
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How can i use timer?

    Open it, read it, add something and write it. For example:

    VB Code:
    1. Dim iFF As Integer
    2. Dim sFile As String
    3. Dim sContents As String
    4.  
    5.     iFF=FreeFile
    6.     sFile="c:\test.txt"
    7.  
    8.                 'read it
    9.         Open sFile For Input As #iFF
    10.             sContents=Input(LOF(iFF),iFF)
    11.         Close #iFF
    12.  
    13.                 'write what was before and add something new
    14.         Open sFile For Output As #iFF
    15.                         Print #iFF, sContents
    16.             Print #iFF, "something new"
    17.         Close #iFF
    edit: or like smuX suggested

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How can i use timer?

    here's another way of writing the array at once:
    VB Code:
    1. Open "C:\data.txt" For Output As #fre
    2.     Print #fre, Join(MyArray, vbCrLf)
    3. Close #fre

  10. #10
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    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

  12. #12
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How can i use timer?

    We do what we can :-)
    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.

  13. #13
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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
  •  



Click Here to Expand Forum to Full Width