Applying What We Have Learned So Far (continued)

johnmora's picture

 

Responding to an Event:

The following code example compiles and runs in Microsoft Visual Studio 2008 C#. A ProgressBar control named progressBar has been added to the form. When the event occurs, the displayed progressBar is incremented by 10% and when it reaches 100% the timer stops and the form closes.

   1: using System;
   2: using System.Windows.Forms;
   3:  
   4: namespace TimerEvents
   5: {   //partial class Timer which derives from Form
   6:     public partial class Timer : Form{
   7:         //declare Timer t
   8:         System.Windows.Forms.Timer t;
   9:  
  10:         public Timer(){
  11:             InitializeComponent();
  12:         }
  13:         //method which responds to the timer tick event
  14:         void t_Tick(object sender, EventArgs e){
  15:             if(progressBar.Value < 100) {
  16:                 progressBar.Value += 10;
  17:             }
  18:             else{
  19:                 t.Stop();
  20:                 Close();
  21:             }
  22:         }
  23:         //this method is fired when the timer initializes.
  24:         private void Timer_Shown(object sender, EventArgs e){
  25:             //initialize Timer t object
  26:             t = new System.Windows.Forms.Timer();
  27:             //set the timer's interval to 1 second (1000 milliseconds)
  28:             t.Interval = 1000;
  29:             //Create the event handler t.Tick
  30:             t.Tick += new EventHandler(t_Tick);
  31:             t.Start();
  32:         }
  33:     }
  34: }

timerEvents

Powered by Drupal - Design by artinet