如何:在 StatusStrip 中以交互方式使用 Spring 属性

更新:2007 年 11 月

可以使用 Spring 属性在 StatusStrip 控件中放置 ToolStripStatusLabel 控件。Spring 属性决定 ToolStripStatusLabel 控件是否自动填充 StatusStrip 控件中的可用空间。

示例

下面的代码示例演示如何使用 Spring 属性在 StatusStrip 控件中放置 ToolStripStatusLabel 控件。Click 事件处理程序执行异或 (XOR) 运算以切换 Spring 属性的值。

若要使用此代码示例,请编译并运行该应用程序,然后单击 StatusStrip 控件上的“Middle (Spring)”(居中(Spring))以切换 Spring 属性的值。

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing


...


' This code example demonstrates using the Spring property 
' to interactively center a ToolStripStatusLabel in a StatusStrip.
Class Form4
    Inherits Form

   ' Declare the ToolStripStatusLabel.
   Private middleLabel As ToolStripStatusLabel


   Public Sub New()
      ' Create a new StatusStrip control.
      Dim ss As 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)")
      AddHandler middleLabel.Click, AddressOf middleLabel_Click
      ss.Items.Add(middleLabel)

      ' Add the rightmost label
      ss.Items.Add("Right")

      ' Add the StatusStrip control to the controls collection.
      Me.Controls.Add(ss)
    End Sub

   ' This event hadler is invoked when the 
   ' middleLabel control is clicked. It toggles
   ' the value of the Spring property.
    Sub middleLabel_Click(ByVal sender As Object, ByVal e As EventArgs)

        ' Toggle the value of the Spring property.
        middleLabel.Spring = middleLabel.Spring Xor True

        ' Set the Text property according to the
        ' value of the Spring property. 
        middleLabel.Text = IIf(middleLabel.Spring, _
        "Middle (Spring - True)", "Middle (Spring - False)")
    End Sub
End Class
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)";
    }
}

编译代码

此示例要求:

  • 对 System.Design、System.Drawing 和 System.Windows.Forms 程序集的引用。

有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic)在命令行上使用 csc.exe 生成。也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例

请参见

参考

ToolStripStatusLabel

StatusStrip

ToolStrip

ToolStripItem

ToolStripMenuItem

其他资源

ToolStrip 控件(Windows 窗体)