Export (0) Print
Expand All

How to: Use the Spring Property Interactively in a StatusStrip 

You can use the Spring property to position a ToolStripStatusLabel control in a StatusStrip control. The Spring property determines whether the ToolStripStatusLabel control automatically fills the available space on the StatusStrip control.

Example

The following code example demonstrates how to use the Spring property to position a ToolStripStatusLabel control in a StatusStrip control. The Click event handler performs an exclusive-or (XOR) operation to switch the value of the Spring property.

To use this code example, compile and run the application, and then click Middle (Spring) on the StatusStrip control to switch the value of the Spring property.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
...    // This code example demonstrates using the Spring property 
    // to interactively center a ToolStripStatusLabel in a StatusStrip.
    class Form4 : Form
    {
        // Declare the ToolStripStatusLabel.
        ToolStripStatusLabel middleLabel;

        public Form4()
        {
            // Create a new StatusStrip control.
            StatusStrip ss = new StatusStrip();

            // Add the leftmost label.
            ss.Items.Add("Left");

            // Handle middle label separately -- action will occur
            // when the label is clicked.
            middleLabel = new ToolStripStatusLabel("Middle (Spring)");
            middleLabel.Click += new EventHandler(middleLabel_Click);
            ss.Items.Add(middleLabel);

            // Add the rightmost label
            ss.Items.Add("Right");

            // Add the StatusStrip control to the controls collection.
            this.Controls.Add(ss);
        }

        // This event hadler is invoked when the 
        // middleLabel control is clicked. It toggles
        // the value of the Spring property.
        void middleLabel_Click(object sender, EventArgs e)
        {
            // Toggle the value of the Spring property.
            middleLabel.Spring ^= true;

            // Set the Text property according to the
            // value of the Spring property. 
            middleLabel.Text = 
                middleLabel.Spring ? "Middle (Spring - True)" : "Middle (Spring - False)";
        }
    }

Compiling the Code

This example requires:

  • References to the System.Design, System.Drawing, and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-Line Building. You can also build this example in Visual Studio by pasting the code into a new project. How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio

See Also

Community Additions

ADD
Show:
© 2015 Microsoft