Home > Articles

This chapter is from the book

Using the Timer Control

The Timer control allows you to execute event-handling code repeatedly in defined intervals. The code will be executed on the same thread that the Windows code is running in. If you need code to run in a thread outside of the Windows thread, then you should use the System.Threading namespace.

The Timer control fires by raising a Tick event. A Tick event is raised only when the Timer's Enabled property is set to true. You can stop a Timer control from firing by setting the Enabled property to false. The Timer control's Interval property defines the number of milliseconds between Tick events.

Trapping the Tap-and-Hold Event

Pocket PC substitutes the tap-and-hold movement in place of the right mouse click. The tap-and-hold movement is simulated by placing the stylus on the screen and holding it in place. A control's ContextMenu control is activated this way.

You may want to respond to a tap-and-hold in other ways beyond bringing up a ContextMenu. To do this, you must use a little creativity. First create a Timer control whose interval is set to the amount of time a user would have to hold the stylus to the screen before triggering the tap-and-hold functionality. Then capture the MouseDown event on the control that will be handling the tap-and-hold event. In the MouseDown event, enable the Timer. Next trap the MouseUp event and disable the Timer in handle. Finally, in the Timer's Tick event handler, perform the action activated by the tap-and-hold event. The following code demonstrates how to handle the three events described above:

C#
private void Form1_MouseDown(object sender,
    System.Windows.Forms.MouseEventArgs e) {
 timer1.Enabled = true;
}

private void Form1_MouseUp(object sender,
    System.Windows.Forms.MouseEventArgs e) {
 timer1.Enabled = false;
}

private void timer1_Tick(object sender, System.EventArgs e) {
 label2.Text = string.Format("Message: {0}", "Hello, World!");
}
VB
private sub _Form1_MouseDown(sender as object,
    e As System.Windows.Forms.MouseEventArgs) _
    Handles form1.MouseDown
 timer1.Enabled = true
End Sub

private sub _Form1_MouseUp(sender as object,
    e As System.Windows.Forms.MouseEventArgs) _
    Handles form1.MouseMove
 timer1.Enabled = false
End Sub

private sub _timer1_Tick(sender as object,
    e As System.Windows.Forms.MouseEventArgs) _
    Handles timer1.Tick
 label2.Text = string.Format("Message: {0}", "Hello, World!")
End Sub

In the "Programming the ProgressBar Control" section, the BombSquad.exe game was presented. The object of the game was to click the Diffuse button before the ProgressBar control filled up. The trick was that the Diffuse button's location would continuously change, while the ProgressBar's value increased. This application uses two timers. One Timer control determined how often the ProgressBar's value was updated. And another Timer control handled moving the Diffuse button around the form. Listing 3.4 demonstrates how these two Timers are handled.

Listing 3.4

C#
private void timerButtonMove_Tick(object sender, System.EventArgs e) {
 if(!gameOver) {
  this.button1.Location = 
   new Point(rand.Next(0, 168), rand.Next(0, 216));
 }else {
  EndGame();
 }
}
private void timerProgressBar_Tick(object sender, System.EventArgs e) {
 if( counter < 60 ) {
  if( gameOver )
   return;

  if(this.progressBar1.Value < this.progressBar1.Maximum)
   this.progressBar1.Value += 1;

   ++counter;
 } else {
  if( !gameOver ) {
   EndGame();
   MessageBox.Show("Boom!", "Loser");
  }
 }
}

private void EndGame() {
 this.timerButtonMove.Enabled = false;
 this.timerProgressBar.Enabled = false;

 counter = 0;
 this.gameOver = true;
 this.button2.Enabled = true;
}
VB
Private Sub timerButtonMove_Tick(ByVal sender As Object,
    ByVal e As System.EventArgs) _ Handles timerButtonMove.Tick
 If Not gameOver Then
  Me.button1.Location = _
   New Point(rand.Next(0, 168), rand.Next(0, 216))
 Else
  EndGame()
 End If
End Sub

Private Sub timerProgressBar_Tick(ByVal sender As Object,
    ByVal e As System.EventArgs) _ Handles timerProgressBar.Tick
 If counter < 60 Then
  If gameOver Then
   Return
  End If

  If Me.progressBar1.Value < Me.progressBar1.Maximum Then
   Me.progressBar1.Value = Me.progressBar1.Value + 1

   counter = counter + 1
  Else
   If Not gameOver Then
    EndGame()
    MessageBox.Show("Boom!", "Loser")
   End If
  End If
 End If
End Sub

Private Sub EndGame()
 Me.timerButtonMove.Enabled = False
 Me.timerProgressBar.Enabled = False

 counter = 0
 Me.gameOver = True
 Me.button2.Enabled = True
End Sub
  • + Share This
  • 🔖 Save To Your Account