Adding Status Bars to Your Application

Most applications have status bars at the bottom of the form for extra information about what is happening with the program. To add a status bar to programs in C#Builder, locate the StatusBar control in the Tool Palette and drag and drop it on the form designer surface. In the default status bar, setting the Text property will make it display a message. For starting off with static text, such as "Ready", set the Text property in the Appearance section of the Object Inspector. Listing 8.5 shows how to set the Text property of a StatusBar control with code.

Listing 8.5 Modifying a StatusBar (StatusBar.cs)
 private void btnSomeProcess_Click(object sender, System.EventArgs e) {    statusBarMain.Text = "Performing Some Process...";    Thread.Sleep(3000);    statusBarMain.Text = "Ready"; } 

The code in Listing 8.5 sets the Text property of the StatusBar to display the simulated status of an operation in progress. The Thread.Sleep statement makes the system wait for 3 seconds before moving on. Remember to add a using statement for the System.Threading namespace at the top of the file.

Many status bars are partitioned into panels for sections of information on various aspects of an application's runtime state. For example, C#Builder has a status bar panel for the code editor that indicates the line and column where the caret is positioned.

To add a panel to a status bar, first add a status bar to the form. Then click on the Panels property in the Appearance section of the Object Inspector, which will display a StatusBarPanel Collection Editor (see Figure 8.10). Add a couple of panels to the status bar. The first one, which could be named statusMain, should have its AutoSize property set to Spring to make it fill any area not taken by other panels. The second panel, which could be called timePanel, should have AutoSize set to the default value of none and its Width property set to 125. It's important to make sure that the ShowPanels property in the Behavior section of the Object Inspector is set to true for the status bar. If the ShowPanels property is not set to true, the status bar panels are not visible.

Figure 8.10. The StatusBarPanel Collection Editor.

graphics/08fig10.jpg

Another thing I'm doing in this example is adding a Timer control to update the timePanel with the current time each second. The Timer can be found on the Tool Palette and dragged to the form designer surface. It will appear in the nonvisual components panel below the designer surface. I gave the Timer a name of statusTimer and implemented its Tick event in Listing 8.6. I also set the Timer control's Interval property in the Behavior section of the Object Inspector to 1000 so that a Tick event would occur every second. Also, be sure to set the Enabled property in the Behavior section of the Object Inspector for the Timer control to true. If the Enabled property is not set to true, the Tick event will not occur.

Listing 8.6 Implementing StatusBarPanel and Timer Controls (StatusBarPanel.cs)
 private void statusTimer_Tick(object sender, System.EventArgs e) {    statusBarMain.Panels[1].Text = DateTime.Now.ToString(); } 

In Listing 8.6, the Tick event handler sets the timePanel status bar panel to the current time every second. To figure out which index to use in the Panels collection of the StatusBar, open the status bar panel editor, found via the Panels property in the Appearance section of the Object Inspector for the StatusBar control. The index corresponds to the number of the timePanel StatusBarPanel in the Members list of the StatusBarPanel editor. Figure 8.11 shows how this program looks when running.

Figure 8.11. Showing panels on StatusBar controls.

graphics/08fig11.jpg



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net