Compartir a través de


Cómo: Utilizar la propiedad Spring de manera interactiva en un control StatusStrip

Puede utilizar la propiedad Spring para colocar un control ToolStripStatusLabel en un control StatusStrip. La propiedad Spring determina si el control ToolStripStatusLabel rellena automáticamente el espacio disponible en el control StatusStrip.

Ejemplo

En el ejemplo de código siguiente se muestra cómo utilizar la propiedad Spring para colocar un control ToolStripStatusLabel en un control StatusStrip. El controlador de eventos Click realiza una operación or-exclusiva (XOR) para cambiar el valor de la propiedad Spring.

Para utilizar este ejemplo de código, compile y ejecute la aplicación y haga clic en Middle (Spring) en el control StatusStrip para intercambiar el valor de la propiedad 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)";
    }
}

Compilar el código

Este ejemplo requiere:

  • Referencias a los ensamblados System.Design, System.Drawing y System.Windows.Forms.

Para obtener información sobre la compilación de este ejemplo desde la línea de comandos de Visual Basic o Visual C#, vea Generar desde la línea de comandos (Visual Basic) o Compilar la línea de comandos con csc.exe. También puede compilar este ejemplo en Visual Studio pegando el código en un proyecto nuevo. Para obtener más información, vea Cómo: Compilar y ejecutar un ejemplo de código completo de formularios Windows Forms utilizando Visual Studio y Cómo: Compilar y ejecutar un ejemplo de código completo de formularios Windows Forms utilizando Visual Studio y Cómo: Compilar y ejecutar un ejemplo de código completo de formularios Windows Forms utilizando Visual Studio y Cómo: Compilar y ejecutar un ejemplo de código completo de Windows Forms en Visual Studio y Cómo: Compilar y ejecutar un ejemplo de código completo de Windows Forms en Visual Studio.

Vea también

Referencia

ToolStripStatusLabel

StatusStrip

ToolStrip

ToolStripItem

ToolStripMenuItem

Otros recursos

ToolStrip (Control de formularios Windows Forms)