Results 1 to 5 of 5

Thread: [2005] Timer Control

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Question [2005] Timer Control

    Hello, i've just got a quick question to do with the Timer Control.
    I haven't been able to get it to work for a while.
    I just don't know how to use it, i've looked at the Microsoft help website but that didn't help me. could anyone explain how to use it and code it. Or could someone add a timer to this code, so that say 1 letter appears every 2 seconds.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim text As System.Diagnostics.Process
    3.         Text = System.Diagnostics.Process.Start("C:\WINDOWS\Notepad.exe")
    4.         text.WaitForInputIdle()
    5.         AppActivate(text.Id)
    6.  
    7.         SendKeys.Send("Hello, this is just a test .")
    8.     End Sub

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Timer Control

    Set the Timer's Interval to 2000 (milliseconds).
    Set its Enabled property to True or call its Start method when you want it to start.
    You will need to store your string in a class level variable and also store a number that indicates the index of the next character.
    In the Timer's Tick event handler you use the character at the current index and then increment the index.
    When all the characters have been sent you set the Timer's Enabled property to False or call its Stop method.

    Having said all that, using SendKeys like that is not a very good idea because if a different application becomes active the key strokes will be sent to it instead.

    By the way, Timer's are not controls. They are components.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Re: [2005] Timer Control

    Thanks for the reply but i didn't understand alot of what you said. Would you be able to post a code example of using a timer in a simple application?

    If sendkeys isn't the best method to use would you be able to list another method?

  4. #4
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: [2005] Timer Control

    VB Code:
    1. Private Idx as integer = 0
    2.     Private strToDisplay as string = "Hello, this is just a test ."
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim text As System.Diagnostics.Process
    5.         Text = System.Diagnostics.Process.Start("C:\WINDOWS\Notepad.exe")
    6.         text.WaitForInputIdle()
    7.         AppActivate(text.Id)
    8.         Idx  = 0
    9.         Me.Timer1.Interval = 2000
    10.         Me.Timer1.Start()
    11.     End Sub
    12.  
    13.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    14.         SendKeys.Send(strToDisplay.substring(Idx,1)
    15.         SendKeys.Flush()
    16.         idx = idx + 1
    17.         if idx > strToDisplay.Length then
    18.            Me.Timer1.Stop()
    19.         End If
    20.     End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    63

    Re: [2005] Timer Control

    Thanks for the code lingsn.

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