Export (0) Print
Expand All

Walkthrough: Updating Status-Bar Information at Run Time

Visual Studio .NET 2003

Often, a program will call for you to update the contents of status-bar panels dynamically at run time, based on changes to application state or other user interaction. This is a common way to signal users that keys such as the CAPS LOCK, NUM LOCK, or SCROLL LOCK are enabled, or to provide the date or a clock as a convenient reference.

In the example below, you will use an instance of the StatusBarPanel class to host a clock that updates as the time changes.

To ready the status bar for updating

  1. Create a new Windows Application. For details, see Creating a Windows Application Project.
  2. Add a StatusBar control to your form. For details, see Adding Controls to Windows Forms.
  3. Add a status-bar panel to your StatusBar control. For details, see Adding Panels to a StatusBar Control.
  4. In the drop-down list at the top of the Properties window, select the StatusBar control you added to your form.
  5. Set the ShowPanels property to true.
  6. Drag a Windows Forms Timer component from the Windows Form tab of the Toolbox to the form. It will be added to the component tray.
    Note   The Windows Forms Timer component is designed for a Windows Forms environment. If you need a timer that is suitable for a server environment, see Introduction to Server-Based Timers.
  7. In the Properties window, set the following properties:
    PropertyValue
    Enabledtrue
    Interval30000
    Note   The Interval property of the Timer component is set to 30 seconds (30,000 milliseconds) to ensure that an accurate time is reflected in the time displayed.

To implement the timer to update the status bar

  1. Double-click the Timer component you added to the form.

    The Code Editor opens with the insertion point in the Timer component's Tick event.

  2. Insert the following code into the event handler to update the panel of the StatusBar control.
    ' Visual Basic
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       StatusBar1.Panels(0).Text = Now.ToShortTimeString
    End Sub
    
    // C#
    private void timer1_Tick(object sender, System.EventArgs e)
    {
       statusBar1.Panels[0].Text = DateTime.Now.ToShortTimeString();
    }
    
    // C++
    private:
      System::Void timer1_Tick(System::Object *  sender,
        System::EventArgs *  e)
      {
        statusBar1->Panels->Item[0]->Text =
          DateTime::Now.ToShortTimeString();
      }
    

To test the application

At this point, you will run the application and observe the clock running in the status-bar panel.

  • Debug the application and press F5 to run it. For details about debugging, see Debugging Basics.
    Note   It will take approximately 30 seconds for the clock to appear in the status bar. This is to get the most accurate time possible. Conversely, to make the clock appear sooner, you could reduce the value of the Interval property you set in step 7 above.

See Also

Adding Panels to A StatusBar Control | Determining Which Panel in the Windows Forms StatusBar Control Was Clicked | Introduction to the Windows Forms StatusBar Control | StatusBar Class

Show:
© 2014 Microsoft