HOW TO:在 StatusStrip 中以互動方式使用 Spring 屬性
更新:2007 年 11 月
您可以使用 Spring 屬性,在 StatusStrip 控制項中放置 ToolStripStatusLabel 控制項。Spring 屬性會判斷 ToolStripStatusLabel 控制項是否會自動填滿 StatusStrip 控制項上的可用空間。
範例
下列程式碼範例會示範如何使用 Spring 屬性,將 ToolStripStatusLabel 控制項放置在 StatusStrip 控制項中。Click 事件處理常式會執行 Exclusive-OR (XOR) 運算,以切換 Spring 屬性的值。
若要使用這個程式碼範例,請編譯並執行應用程式,然後按一下 StatusStrip 控制項上的 [Middle (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 中建置此範例。