Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C# StatusStrip Control Explained

1. Introduction to C# StatusStrip Control

StatusStrip’ is a group of strip controls like MenuStrip and ToolbarStrips. Application Developers uses the StatusStrips to display some quick status data to the user. A C# StatusStrip Control will look like the one shown below:

C# StatusStrip At a Glance

C# StatusStrip At a Glance

C# StatusStrip Control allows us to add even combo and text boxes to the status. In this example, we will learn how to add a Status Bar to a C# Application with three status slots in it. Then we will look at some important properties and use that efficiently.

2. About The Example

The screenshot of the Example is below:

C# StatusStrip Example

C# StatusStrip Example

In the above example, the black colored narrow strip is the StatusStrip, and it has three slots in it. The computation section will calculate the sum of the number which we enter inside the text box. On iteration of the numbers, a delay is introduced to simulate that it is a long-running process. During this time, ‘Computing please wait’ will appear in the first status slot. The second slot will show current border style of the status bar and the third one shows user’s last input given in the text box.

We can set two kinds of border style to the status bar using the Border Style section. After picking the border style, one can apply that style to top or left or even all four edges of the status bar using the ‘Apply To’ section of the example. When user removes the check mark, we will revoke the style.

The spring check boxes allow expanding the slot to occupy the remaining free space. We will start our Example now.

3. Adding Status Bar and Status Slots

One can spot the StatusStrip control in the Menus & Toolbar section of the Toolbox in the Visual Studio. First, we should add this control to the form. Second, we add three status labels to it using the ‘Edit Items’ option of the status bar. Then inside the property dialog, we set the name for each status slot. This is shown in the below video:

4. Assigning Initial Texts in The Status

We can access the StatusLabel in the status bar directly by its name. In the form load event handler, we are placing some initial text to it. Below is the event handler code:

SLabel1

to

SLabel3

is added in the previous step and its text property is set during the form load. So, the initial display of the form will show above set texts in the status labels. The below video shows the status bar with initial values:

5. Showing Informative Text in Status Bar

When is it required to show informative text? Say when the application is doing a long-running task, we may need to inform this to the user. Some people display an hourglass icon and some display a ‘Please Wait…’ message in the status bar. We will display this message in the Status Bar. The compute button will calculate the summation of number entered by the user. We use ‘

Thread.Sleep

’ to mimic this as a long-running task. The ‘Compute Summation’ button click handler is below:

In the above code, the

Thread.Sleep(10);

is used to hang the sample for 10 milliseconds in each iteration. Before the simulated long process starts, we are displaying the status message using the statement:

SLabel1.Text = Computing Please Wait..;

and at the end, we are changing the status label to ‘Idle’ meaning that the application is not busy. Also, we are setting the input given by the user in the third status slot.

We should note that even though we set the status label ‘Computing Please Wait…’, it will not get displayed when the application is too busy. This means, when the application is busy, it has no time to render any user interface changes. Hence, displaying information text in the status label text appears only when the application goes back to normal state. In effect, our user never sees the message, ‘Computing Please Wait…’ in the First Status Slot. To avoid this, we call

Application.DoEvents()

. It will notify windows to process any pending UI redraw. In our case, windows form will see that it needs to render the text, ‘Computing Please Wait…’ in status slot. How the status label informs the busy message to the user is shown in the below video:

6. Applying and Revoking Border Style of the Status Slots

Note that in section 3 of this article we added three

ToolStripStatusLabels

 to our C# StatusStrip Control. In this section, we will set the border style to the edges of these status labels.

The table below shows all the available ‘Border Styles’. In this Example, we will take only two of them for testing. In this section, we will see how to apply the ‘Border Styles’ and then how to revoke it.

Member nameDescription
AdjustThe border is drawn outside the specified rectangle, preserving the dimensions of the rectangle for drawing.
BumpThe inner and outer edges of the border have a raised appearance.
EtchedThe inner and outer edges of the border have an etched appearance.
FlatThe border has no three-dimensional effects.
RaisedThe border has raised inner and outer edges.
RaisedInnerThe border has a raised inner edge and no outer edge.
RaisedOuterThe border has a raised outer edge and no inner edge.
SunkenThe border has sunken inner and outer edges.
SunkenInnerThe border has a sunken inner edge and no outer edge.
SunkenOuterThe border has a sunken outer edge and no inner edge.

6.1 ToolStripStatusLabelBorderSides Enumeration

ToolStripStatusLabelBorderSides

Enumeration specifies which side of the status slot is having the border style. The below table describes enumeration values:

ConstantValueDescription
All15All sides of the ToolStripStatusLabel have borders.
Bottom8Only the bottom side of the ToolStripStatusLabel has borders.
Left1Only the left side of the ToolStripStatusLabel has borders.
None0The ToolStripStatusLabel has no borders.
Right4Only the right side of the ToolStripStatusLabel has borders.
Top2Only the top side of the ToolStripStatusLabel has borders.

First, we declare above said Enumeration type as a class member. This member will represent the side in which the user wants to apply the border style. We set the default value as

None

. This means no border (refer the table above).

6.2 Border Style Radio Buttons

When the user selects the Border Style using either ‘Sunken outer’ or ‘Etched’ radio buttons, we are making a call to the user-defined function ‘ApplyBorderStyle’. Note, we wrote event handler for one radio button only. Since the event is ‘checked-changed’, C# will call this when a user changes the check state of these radio buttons. Code is below:

6.3 BorderSides and BorderStyle Properties

In the ApplyBorderStyle function, we set the

BorderSides

property to all three status slots. The

applyto

variable will hold the edges that requires border decoration. The checkbox event handler (not yet written) will assign or revoke the sides from this variable. After setting the

BorderSides

property, we apply the border style to all three status slots by setting the

BorderStyle

property. Note, the

Border3DStyle

 enumeration supplies the border style to this property. Finally, we update the status slot 2 to inform the current border style to the user.

6.4 Apply And Revoke Border Style

To apply or revoke the sides in effect, we use the

bitwise OR(|)

,

bitwise XOR(^)

. The below example shows how these operators are used to apply or revoke Border Style from the top edge of Status Slots:

To apply to top side:

To revoke from top side:

applyto = applyto ^ ToolStripStatusLabelBorderSides.Top;

We use the above bitwise operations when our users check and uncheck the controls under the ‘Apply To’ group box. These check boxes specify the edges in which we will apply the border style. Below are the border sides, which is taken from the

ToolStripStatusLabelBorderSides

 enumeration:

The above two statement that applies and then revokes Border Style on the Top border side is depicted as follows:

Bitwise Operation To Apply C# StatusStrip Border Style

Bit-wise Operation To Apply C# StatusStrip Border Style

The checkbox event handler will apply and revoke the corresponding edges using the method pointed out above. Note, after storing the required sides in the variable ‘

applyto

’, we make a call to the ‘

ApplyBorderStyle()

’. We already saw that the called function will apply user selected border styles to the applicable edges. The code for that is below:

Below is the video that shows testing the Border style code explained above:

7. RenderMode Property of StatusStrip

The Render Mode is just a different style for the status bar. With the current background of the status bar, the Render Mode has a very little effect. Below is the code that makes use of the ‘

RenderMode

Property’:

8. Spawn Status Slot Through Spring Property

As the name suggests, it performs the spring action on the status slots. When the ‘

Spring Property

’ is set to true for a slot, it expands to occupy the remaining free area in the status bar. When more than one slot has

Spring

 property set to true, they share the remaining free space on the StatusStrip together.

Below is the checkbox handler code for setting the spring property for either first slot or second slot or for both.  We intentionally do not provide this option for the third slot so that we can know how the third slot always fit the text it is holding.

The below video shows how the Spring Property expands the Status Slot in Status Strip:

The sample is in VS2005 format. If you have the latest one, just accept the conversion asked by the dialog of your latest IDE.

Download Link From Google Drive: C# StatusStrip Control Example

Categories: C#

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.