Previous Section  < Day Day Up >  Next Section

7.6. The ProgressBar, Timer, and StatusStrip Classes

The ProgressBar and Timer are lightweight controls that have complementary roles in an application: The Timer initiates action and the ProgressBar reflects the status of an operation or action. In fact, the Timer is not a control, but a component that inherits from the ComponentModel.Component class. It is used most often in processes to regulate some background activity. This may be a periodic update to a log file or a scheduled backup of data. A ProgressBar, on the other hand, provides visual feedback regarding the progress of an operation梥uch as file copying or steps in an installation.

The third class discussed in this section is the StatusStrip, which is often used in conjunction with a timer and ProgressBar. It's rendered on a form as a strip divided into one or more sections or panes that provide status information. Each section is implemented as a control that is added to the StatusStrip container. For a control to be included in the StatusStrip, it must inherit from the ToolStripItem class.

Building a StatusStrip

Let's now build a form that includes a multi-pane StatusStrip. As shown in Figure 7-15, the strip consists of a label, progress bar, and panel controls. The label (ToolStripLabel) provides textual information describing the overall status of the application. The progress bar is implemented as a ToolStripProgressBar object. It is functionally equivalent to a ProgressBar, but inherits from ToolStripItem. A StatusStripPanel shows the elapsed time since the form was launched. An event handler that is triggered by a timer updates both the progress bar and clock panel every five seconds.

Figure 7-15. StatusStrip with Label, ProgressBar, and Panel

Listing 7-5 contains the code to create the StatusStrip. The left and right ends of the progress bar are set to represent the values 0 and 120, respectively. The bar is set to increase in a step size of 10 units each time the PerformStep method is executed. It recycles every minute.

The Timer controls when the bar is incremented and when the elapsed time is updated. Its Interval property is set to a value that controls how frequently its Tick event is fired. In this example, the event is fired every 5 seconds, which results in the progress bar being incremented by 10 units and the elapsed time by 5 seconds.

Listing 7-5. StatusStrip That Uses a ProgressBar and Timer

// These variables have class scope

Timer currTimer;

StatusStrip statusStrip1;

StatusStripPanel panel1;

ToolStripProgressBar pb;

DateTime startDate = DateTime.Now;

private void BuildStrip()


   currTimer = new Timer();

   currTimer.Enabled = true;

   currTimer.Interval = 5000; // Fire tick event every 5 seconds

   currTimer.Tick += new EventHandler(timer_Tick);

   // Panel to contain elapsed time

   panel1 = new StatusStripPanel();

   panel1.BorderStyle = Border3DStyle.Sunken;

   panel1.Text = "00:00:00";

   panel1.Padding = new Padding(2);

   panel1.Name = "clock";

   panel1.Alignment = ToolStripItemAlignment.Tail; //Right align

   // Label to display application status

   ToolStripLabel ts = new ToolStripLabel();

   ts.Text = "Running...";        

   // ProgressBar to show time elapsing

   pb = new ToolStripProgressBar();

   pb.Step    = 10;      // Size of each step or increment

   pb.Minimum = 0;

   pb.Maximum = 120;     // Allow 12 steps

   // Status strip to contain components

   statusStrip1 = new StatusStrip(); 

   statusStrip1.Height = 20;

   statusStrip1.AutoSize = true;

   // Add components to strip

   statusStrip1.Items.AddRange(new ToolStripItem[] {

      ts, pb, panel1 } );



private void timer_Tick(object sender, EventArgs e)


   // Get difference between current datetime 

   // and form startup time

   TimeSpan ts = DateTime.Now.Subtract(startDate);

   string elapsed = ts.Hours.ToString("00") + ":" + 

         ts.Minutes.ToString("00") + 

         ":" + ts.Seconds.ToString("00");


         "clock"]).Text= elapsed;

   // Advance progress bar

   if (pb.Value == pb.Maximum) pb.Value = 0;

   pb.PerformStep();   // Increment progress bar


The StatusStripPanel that displays the elapsed time has several properties that control its appearance and location. In addition to those shown here, it has an Image property that allows it to display an image. The StatusStripPanel class inherits from the ToolStripLabel class that is used in the first pane. Both can be used to display text, but the panel includes a BorderStyle property that ToolStripLabel lacks.

    Previous Section  < Day Day Up >  Next Section