Results 1 to 2 of 2

Thread: Can a timer be started as a thread and execute with KeyDown?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Red face Can a timer be started as a thread and execute with KeyDown?

    Hi,

    Can a timer tick event be started as a seperate thread and be executed concurrently with the KeyDown event?

    When I press the Up arrow key, a picturebox1 has to go up and down the form vertically (jumping). At the same time, I would like the timer tick event to continue to move another picturebox2 from right to left on the form by 1 pixel for each tick event.

    Initially, these 2 events cant be dealt with concurrently by vb, so I used the Application.DoEvent() method recommended by some replies I have received in the forums.

    It works partially. Pictuerbox2 can move while the up arrow key is being pressed but it moves jerkily. Besides, when the arrow key is being released, picturebox1 continues to jump when it is supposed to stop.

    So can I start a new thread which can execute when these 2 events occur?
    If so, could you provide a simple example?

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Can a timer be started as a thread and execute with KeyDown?

    Quote Originally Posted by LiLo
    Hi,

    Can a timer tick event be started as a seperate thread and be executed concurrently with the KeyDown event?

    When I press the Up arrow key, a picturebox1 has to go up and down the form vertically (jumping). At the same time, I would like the timer tick event to continue to move another picturebox2 from right to left on the form by 1 pixel for each tick event.

    Initially, these 2 events cant be dealt with concurrently by vb, so I used the Application.DoEvent() method recommended by some replies I have received in the forums.

    It works partially. Pictuerbox2 can move while the up arrow key is being pressed but it moves jerkily. Besides, when the arrow key is being released, picturebox1 continues to jump when it is supposed to stop.

    So can I start a new thread which can execute when these 2 events occur?
    If so, could you provide a simple example?
    Hi,

    You can use the KeyDown event and execute then your code;

    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.        
    3.         If e.KeyCode = Keys.Up Then
    4.             ' execute here your code to change the location of your TextBox
    5.       else
    6.           Timer1.Enable = False                      
    7.           End If
    8.        
    9.     End Sub

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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