Spotlight on the StatusStrip Control

September 12th, 2012 by David Corrales
Last updated on September 12th, 2012

 

The “Spotlight on Controls” series focuses on a single WinForms control in PowerShell Studio 2012, details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.

Note: The StatusStrip control is only available for PowerShell Studio 2012 v3.0.6 or greater

Last time we took a look at the StatusBar control. This time we will look at the StatusStrip control:

StatusStrip[System.Windows.Forms.StatusStrip]

Represents a Windows status bar control.

StatusStrip Toolbox Icon

Default Event: ItemClick

Why use a StatusStrip control?

Use the StatusStrip control, to display status information to the user. The StatusStrip serves as a replacement to the StatusBar and also offers more support for user interaction than the StatusBar by allowing you to add buttons, progress bars, and other controls.

Important Properties:

AllowItemReorder

This property allows the items in the StatusStrip to be reordered when the ALT key is pressed.

 

SizingGrip

This property indicates whether a sizing handle (grip) is displayed in the lower-right corner of the control. This property is mainly cosmetic and should be set to False when using fixed sized forms, such as when the form’s FormBorderStyle property is set to FixedDialog. For more details about the Form control and the FormBorderStyle property, please refer to the Spotlight on the Form Control article.

Default Value: True

SizingGrip

 

ShowItemToolTips

This property indicates whether ToolTips are shown for the StatusStrip. Set this property to True when you want to provide help information for items in the StatusStrip.

Default Value: False

ShowItemToolTips

 

LayoutStyle

This property indicates how the StatusStrip lays out the items collection.

Values (Default: Table):

StackWithOverflow
Specifies that items are laid out automatically.

HorizontalStackWithOverflow
Specifies that items are laid out horizontally and overflow as necessary.

VerticalStackWithOverflow
Specifies that items are laid out vertically, are centered within the control, and overflow as necessary.

Flow
Specifies that items flow horizontally or vertically as necessary.

Table
Specifies that items are laid out flush left.

Note: If you want to ensure all the items on the status bar are visible even when the form is resized then you can use the Flow layout style:

Flow Layout

Otherwise the items that do not fit in the StatusStrip (depending on form size) will be hidden from view.

Dock

This property indicates which StatusStrip borders are docked to its parent control and determines how a StatusStrip is resized with its parent.

Values (Default: Bottom):

None
The control is not docked. The StatusStrip becomes a free floating control that cannot be moved by the user.

Top
The control’s top edge is docked to the top of its containing control.

Bottom
The control’s bottom edge is docked to the bottom of its containing control.

Left
The control’s left edge is docked to the left edge of its containing control.

Right
The control’s right edge is docked to the right edge of its containing control.

Fill
All the control’s edges are docked to the all edges of its containing control and sized appropriately.

Docking

 

Items

This property contains all the items that belong to the StatusStrip. This property consists of a collection of ToolStripItems, which come in various flavors. For more information, refer to the ToolStripItem Types section below.

Adding Items to the StatusBar

Using the Designer:

You can add ToolStripItems directly in the designer by accessing the dropdown inside the StatusStrip:

Add Items Directly in Designer

Note: To edit the item’s properties you will have to select the ToolStripItem and use the Property Panel.

You can also use the Item Collection Editor to add items to the StatusStrip.

Access the Item Collection Editor using the property panel:

Property Pane

Or the by using the designer menu:

Designer menu

The Item Collection Editor allows you to add / remove ToolStripItems and alter their properties in a single location:

Items Collection Editor

 

Using the Script Editor:

You can add ToolStripItems using the Item property’s Add methods:

[ToolStripItem] Add ([Image] image)
Adds a ToolStripItem that displays the specified image to the collection.

[ToolStripItem] Add ([String] text)
Adds a ToolStripItem that displays the specified text to the collection.

[int] Add ([ToolStripItem] value)
Adds the specified item to the end of the collection.

[ToolStripItem] Add ([String] text, [Image] image)
Adds a ToolStripItem that displays the specified image and text to the collection.

[ToolStripItem] Add ([String] text, [Image] image, [EventHandler] onClick)
Adds a ToolStripItem that displays the specified image and text to the collection and that raises the Click event.

Example Code:

$clickEvent = {
    Write-Host 'Click'
}

$image = [System.Drawing.Image]::FromFile('c:\image.png')

$statusstrip1
.Items.Add('Text', $image , $clickEvent)

 

Important Events:

ItemClicked

This event occurs when a ToolStripItem is clicked. This event allows you to handle all the click events in one location, but it is not recommend using this event if you are have DropDownButtons or SplitButtons. Instead you should use each ToolStripItem’s individual event handlers.

$statusstrip1_ItemClicked=[System.Windows.Forms.ToolStripItemClickedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.ToolStripItemClickedEventArgs]

    Write-Host $_.ClickedItem.Text
    if($_.ClickedItem -eq $toolstripstatuslabel1)
    {
        Write-Host "You clicked the label"
    } 
}

 

ToolStripItem Types:

You can add a variety of items to the StatusStrip, which are presented below.

The following properties are shared with all the types of ToolStripItems, with the exception of the ProgressBar which only shares the ToolTipText property:

Important Shared Properties:

Property Description
ToolTipText

Specifies the text to show on the Tooltip.
If this property is not set and the AutoToolTip property is set to True, the item will use the Text property as its ToolTip.

DisplayStyle Specifies whether text and images are displayed on an item.
Values:

None
Specifies that neither image nor text is to be rendered for this item.

Text
Specifies that only text is to be rendered for this item.

Text Only

Image
Specifies that only an image is to be rendered for this item.

Image Only

ImageAndText
Specifies that both an image and text are to be rendered for this item.

Image and Text

Image The image that will be displayed on the item.
ImageScaling Indicates whether the image on the item will automatically resize to fit in the StatusStrip.
Values:

None
Specifies that the size of the image on an item is not automatically adjusted to fit on a StatusStrip. The image retains its original size.

ImageScaling None

SizeToFit
Specifies that the size of the image on an item is automatically adjusted to fit on a StatusStrip.

ImageScaling SizeToFit

Text The text that is to be displayed on the item.
TextImageRelation The position of the item’s text and image relative to each other.
Values:

Overlay
Specifies that the image and text share the same space on a control.

TextImageRelation Overlay

ImageBeforeText
Specifies that the image is displayed horizontally before the text of a control.
TextImageRelation Image Before

TextBeforeImage
Specifies that the text is displayed horizontally before the image of a control.
TextImageRelation Text Before

ImageAboveText
Specifies that the image is displayed vertically above the text of a control.
TextImageRelation Image Above

TextAboveImage
Specifies that the text is displayed vertically above the image of a control.
TextImageRelation Text Above

DoubleClickEnabled Indicated whether the item can be activated by double-clicking the mouse.
Values: True / False

Important Shared Events:

The following events are shared with all the types of ToolStripItems:

Event Description
Click Occurs when the item is clicked.
DoubleClick Occurs when the item is clicked.
Note: Order for this event to be triggered, some types require that the DoubleClickEnabled property to be set to True.

 

The following is a list of ToolStripItems types that can be added to the StatusStrip control:

StatusLabel [System.Windows.Forms.ToolStripStatusLabel]

Status Label

Represents a panel in a StatusStrip control. Use the StatusLabel to show text and/or an image.

Important Properties:

Properties Description
BorderSides Indicates which sides of the ToolStripStatusLabel show borders.
Values:
None, Top, Left, Right, Bottom, All
BorderSides - Right
BorderSide - All
BorderStyles Indicates the border style of the ToolStripStatusLabel.

Values (Default: Flat):

Adjust
The border is drawn outside the specified rectangle, preserving the dimensions of the rectangle for drawing.

 Bump
The inner and outer edges of the border have a raised appearance.

 Etched
The inner and outer edges of the border have an etched appearance.

 Flat
The border has no three-dimensional effects.

 Raised
The border has raised inner and outer edges.

 RaisedInner
The border has a raised inner edge and no outer edge.

 RaisedOuter
The border has a raised outer edge and no inner edge.

Sunken
The border has sunken inner and outer edges.

SunkenInner
The border has a sunken inner edge and no outer edge.

SunkenOuter
The border has a sunken outer edge and no inner edge.

BorderStyles

Spring Indicates whether the ToolStripStatusLabel automatically fills the available space on the StatusStrip as the form is resized.

Values: True / False

Spring

IsLink Indicated whether the ToolStripLabel is a hyperlink.

Values: True / False

Label as Link

LinkBehavior Indicates the behavior of a link.

Values (Default: SystemDefault):

SystemDefault
The behavior of this setting depends on the options set using the Internet Options dialog box in Control Panel or Internet Explorer.

AlwaysUnderline
The link always displays with underlined text.

HoverUnderline
The link displays underlined text only when the mouse is hovered over the link text.

NeverUnderline
The link text is never underlined. The link can still be distinguished from other text by use of the LinkColor property.

 

Making a Link Functional:

In order to have the Label open a webpage you will need to handle the label’s Click event:

$toolstripstatuslabelLink_Click={
    #Open the URL in the Default Application
    [System.Diagnostics.Process]::Start($this.Text)
}

In the case where you don’t want the text to show the actual link, you can instead store the link in the label’s Tag property:

Tag Property

And modify the Click event as follows:

$toolstripstatuslabelLink_Click={
    #Show the URL stored in the TAG property
    if($this.Tag -is [string])
    {
        [System.Diagnostics.Process]::Start($this.Tag)
    }
}

 

ProgressBar [System.Windows.Forms.ToolStripProgressBar]

ProgressBar

Represents a Windows progress bar control contained in a StatusStrip. Use the Progress bar to show the total progress of an operation.

Important Properties:

Properties Description
Maximum The upper bound of the range that is defined for this ToolStripProgressBar.
Minimum The lower bound of the range that is defined for this ToolStripProgressBar.
Step The amount by which to increment the current value of the ToolStripProgressBar when the PerformStep method is called.
Value Indicates the current value of the ToolStripProgressBar.

Please refer to the Spotlight on the ProgressBar Control blog article for more details on the progress bar.

 

DropDownButton [System.Windows.Forms.ToolStripDropDownButton]

DropDownButton

Represents a button that when clicked displays an associated menu from which the user can select a single item.

Important Properties:

Properties Description
DropDown Specifies a ToolStripDropDown (such as a ContextMenuStrip) that will be displayed when this item is clicked.
DropDownItems Specifies a ToolStripItem to display when this item is clicked.
ShowDropDownArrow Specifies whether an arrow is displayed on the item, which indicates that further options are available in a drop-down list.

Important Events:

Properties Description
DropDownItemClicked Occurs when the ToolStripDropDown is clicked.

$toolstripdropdownbutton2_DropDownItemClicked=[System.Windows.Forms.ToolStripItemClickedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.ToolStripItemClickedEventArgs]
    Write-Host $_.ClickedItem.Text
}

Please refer to the Spotlight on the ContextMenuStrip Control blog article for more details on the menus.

Making an ordinary button:

Normal Button
In order to make a DropDownButton function like an ordinary button, you must do the following:

1. Set the ShowDropDownArrow property to False.
2. Clear any DropDown Items from the button.
3. Create an event handler for the DropDownButton’s Click event.

SplitButton [System.Windows.Forms.ToolStripSplitButton]

SplitButton

Represents a combination of a standard button on the left and a drop-down button on the right, or the other way around if the value of RightToLeft is Yes.

Important Properties:

Properties Description
DropDown Specifies a ToolStripDropDown (such as a ContextMenuStrip) that will be displayed when this item is clicked.
DropDownItems Specifies a ToolStripItem to display when this item is clicked.

 

Important Events:

Properties Description
Click Occurs when the button is clicked.
DropDownItemClicked Occurs when the ToolStripDropDown is clicked.

 

Please refer to the Spotlight on the ContextMenuStrip Control blog article for more details on the menus.

 

 
[Google+]   [Facebook]   [LinkedIn]   [StumbleUpon]   [Digg]   [Reddit]   [Google Bookmark]  

Tags: , , , ,