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.

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)";
    }
}

Show:
© 2014 Microsoft