Procedura: utilizzare la proprietà Spring in modo interattivo in StatusStrip

È possibile utilizzare la proprietà Spring per posizionare un controllo ToolStripStatusLabel in un controllo StatusStrip. La proprietà Spring determina se il controllo ToolStripStatusLabel riempie automaticamente lo spazio disponibile nel controllo StatusStrip.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare la proprietà Spring per posizionare un controllo ToolStripStatusLabel in un controllo StatusStrip. Il gestore eventi Click esegue un'operazione con OR esclusivo (XOR) per modificare il valore della proprietà Spring.

Per utilizzare questo esempio di codice, compilare ed eseguire l'applicazione, quindi scegliere Middle (Spring) sul controllo StatusStrip per modificare il valore della proprietà 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)";
    }
}

Compilazione del codice

Per questo esempio sono necessari i seguenti requisiti:

  • Riferimenti agli assembly System.Design, System.Drawing e System.Windows.Forms.

Per informazioni sulla compilazione di questo esempio dalla riga di comando per Visual Basic o Visual C#, vedere Building from the Command Line (Visual Basic) o Compilazione dalla riga di comando con csc.exe. È anche possibile compilare questo esempio in Visual Studio incollando il codice in un nuovo progetto. Per ulteriori informazioni, vedere Procedura: compilare ed eseguire un esempio di codice Windows Form completo tramite Visual Studio e Procedura: compilare ed eseguire un esempio di codice Windows Form completo tramite Visual Studio e Procedura: compilare ed eseguire un esempio di codice Windows Form completo tramite Visual Studio e Procedura: compilare ed eseguire un esempio di codice Windows Form completo tramite Visual Studio e Procedura: compilare ed eseguire un esempio di codice Windows Form completo tramite Visual Studio.

Vedere anche

Riferimenti

ToolStripStatusLabel

StatusStrip

ToolStrip

ToolStripItem

ToolStripMenuItem

Altre risorse

Controllo ToolStrip (Windows Form)