StatusBar


The StatusBar is an easy-to-use control for displaying status information to the user. You will find the status bar at the bottom of many Windows applications. There are two approaches to the status bar provided by the .NET Framework class library. You can use that status bar as one long text area within which you can place your status information, or you can break the status bar into panels. Each of these panels can be updated independently.

In most cases when you work with the StatusBar, you will only have to deal with three properties. If you are working with the status bar as one long area to enter text into, then you only really have to worry about placing your status information into the Text property. If, on the other hand, you are breaking your status bar into panels, you need to work with Panels property, which you will place your collection of StatusBarPanels components and the ShowPanels property, which turns these panels on and off.

Creating a status bar as a single long text area is trivial. Either drag the StatusBar control to the design form or enter the following code:

 private: StatusBar *statusbar; //...In constructor StatusBar = new StatusBar(); Controls->Add(statusBar); //...Later when you want to update the status StatusBar->Text = S"status info goes here"; 

Creating a status bar using panels is not much more difficult, but the StatusBarPanel does have a number of properties available for configuration. Some of the more common properties are as follows:

  • Alignment is a HorizontalAlignment enum that represents how the text and/or icon are aligned (Left, Right, or Center) within the panel. The default is HorizonalAlignment::Left. It should be noted that there is no way of aligning the text and the icon separately, because the icon will always be positioned to the left of the text.

  • AutoSize is a StatusBarPanelAutoSize enum that represents whether the panel is a fixed size (None), fits to the text and or icon content of the panel (Content), or adjusts to the slack space (Spring). The default is StatusBarPanelAutoSize::None, meaning that the panel is a fixed size.

  • BorderStyle is a StatusBarPanelBorderStyle enum that represents the border style of the panel. The default is no border being displayed or StatusBarPanelBorderStyle::None. Other options are Raised and Sunken.

  • Icon is an Icon that represents the icon to display on the panel.

  • MinWidth is an Int32 that represents the minimum allowable width for the panel in pixels. The value must be greater than zero. This property is used in conjunction with the AutoSize property so that a panel will not autosize to one that is too small.

  • Parent is a StatusBar that represents the parent of the panel.

  • Text is a String that represents the text displayed on the panel.

  • ToolTip is a String that represents the ToolTip associated with the panel.

  • Width is an Int32 that represents the width of the panel in pixels. The value must be greater than zero. If AutoSize is enabled, then this property reflects the actual autosized width of the panel.

Listing 10-6 shows the creation of the status bar with panels. The status information displayed is the mouse x, y location and the last mouse button pressed while within the client area of the form.

Listing 10-6: Status Bar Display of x, y Coordinates

start example
 namespace StatusBar1 {     using namespace System;     using namespace System::ComponentModel;     using namespace System::Collections;     using namespace System::Windows::Forms;     using namespace System::Data;     using namespace System::Drawing;     public __gc class Form1 : public System::Windows::Forms::Form     {     public:         Form1(void)         //...     protected:         void Dispose(Boolean disposing)         //...     private: System::Windows::Forms::StatusBar *  statusBar;     private: System::Windows::Forms::StatusBarPanel *  statusButtons;     private: System::Windows::Forms::StatusBarPanel *  statusXCoord;     private: System::Windows::Forms::StatusBarPanel *  statusYCoord;     private: System::ComponentModel::Container *  components;         void InitializeComponent(void)         {             this->statusBar = new System::Windows::Forms::StatusBar();             this->statusButtons = new System::Windows::Forms::StatusBarPanel();             this->statusXCoord = new System::Windows::Forms::StatusBarPanel();             this->statusYCoord = new System::Windows::Forms::StatusBarPanel();             (dynamic_cast<System::ComponentModel::ISupportInitialize * >             (this->statusButtons))->BeginInit();             (dynamic_cast<System::ComponentModel::ISupportInitialize * >             (this->statusXCoord))->BeginInit();             (dynamic_cast<System::ComponentModel::ISupportInitialize * >             (this->statusYCoord))->BeginInit();             this->SuspendLayout();             //             // statusBar             //             this->statusBar->Name = S"statusBar";             System::Windows::Forms::StatusBarPanel* _mcTemp_1[] =                  new System::Windows::Forms::StatusBarPanel*[3];             __mcTemp__1[0] = this->statusButtons;             __mcTemp__1[1] = this->statusXCoord;             __mcTemp__1[2] = this->statusYCoord;             this->statusBar->Panels->AddRange(_mcTemp_1);             this->statusBar->ShowPanels = true;             //             // statusButtons             //             this->statusButtons->AutoSize =                   System::Windows::Forms::StatusBarPanelAutoSize::Spring;             //             // statusXCoord             //             this->statusXCoord->Width = 50;             //             // statusYCoord             //             this->statusYCoord->Width = 50;             //             // Form1             //             this->AutoScaleBaseSize = System::Drawing::Size(6, 15);             this->ClientSize = System::Drawing::Size(300, 322);             this->Controls->Add(this->statusBar);             this->Name = S"Form1";             this->Text = S"Status Bar Mouse Tracking";             this->MouseDown +=         new System::Windows::Forms::MouseEventHandler(this, Form1_MouseDown);             this->MouseMove +=         new System::Windows::Forms::MouseEventHandler(this, Form1_MouseMove);             (dynamic_cast<System::ComponentModel::ISupportInitialize * >             (this->statusButtons))->EndInit();             (dynamic_cast<System::ComponentModel::ISupportInitialize * >             (this->statusXCoord))->EndInit();             (dynamic_cast<System::ComponentModel::ISupportInitialize * >             (this->statusYCoord))->EndInit();             this->ResumeLayout(false);         }     private:         System::Void Form1_MouseMove(System::Object *  sender,                       System::Windows::Forms::MouseEventArgs *  e)         {             // x,y coords in second and third status bar panels             statusXCoord->Text = String::Format("X={0}", __box(e->X));             statusYCoord->Text = String::Format("Y={0}", __box(e->Y));         }     private:         System::Void Form1_MouseDown(System::Object *  sender,                       System::Windows::Forms::MouseEventArgs *  e)         {             // clicked mouse button in first status bar panel             if (e->Button == MouseButtons::Right)                 statusButtons->Text = S"Right";             else if (e->Button == MouseButtons::Left)                 statusButtons->Text = S"Left";             else                 statusButtons->Text = S"Middle";         }     }; } 
end example

The preceding code builds a three-panel status bar. The first panel expands to take up any unclaimed area on the status bar due to StatusBarPanelAutoSize::Spring. The other two panels are 50 pixels wide. All of the panels are added to the StatusBar, which in turn is added to the form. The only tricky part of this program is remembering to set the property ShowPanels to true.

Figure 10-8 shows what StatusBar.exe looks like when you execute it.

click to expand
Figure 10-8: A three-panel status bar




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

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