방법: StatusStrip에서 대화형으로 Spring 속성 사용

Spring 속성을 사용하여 ToolStripStatusLabel 컨트롤을 StatusStrip 컨트롤에 배치할 수 있습니다. Spring 속성은 ToolStripStatusLabel 컨트롤이 StatusStrip 컨트롤에서 사용 가능한 공간을 자동으로 채울지 여부를 결정합니다.

예제

다음 코드 예제에서는 Spring 속성을 사용하여 ToolStripStatusLabel 컨트롤을 StatusStrip 컨트롤에 배치하는 방법을 보여 줍니다. Click 이벤트 처리기는 XOR(배타적 OR) 연산을 수행하여 Spring 속성의 값을 전환합니다.

이 코드 예제를 사용하려면 애플리케이션을 컴파일하고 실행한 다음, StatusStrip 컨트롤에서 Middle (Spring)을 클릭하여 Spring 속성의 값을 전환합니다.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
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 : 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)";
    }
}
' 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

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • System.Design, System.Drawing 및 System.Windows.Forms 어셈블리에 대한 참조

참고 항목