次の方法で共有


方法: ToolStripTextBox をストレッチして ToolStrip の残りの幅を埋める (Windows フォーム)

Stretch コントロールの ToolStrip プロパティを trueに設定すると、コントロールはコンテナーを端から端まで埋め、コンテナーのサイズが変更されたときにサイズも調整します。 この構成では、コントロール内の項目 (ToolStripTextBoxなど) を拡張して、使用可能な領域を埋め、コントロールのサイズを変更するときにサイズを変更すると便利な場合があります。 このストレッチは、たとえば、Microsoft® Internet Explorer のアドレス バーのような外観と動作を実現する場合に便利です。

次のコード例では、ToolStripTextBoxと呼ばれる ToolStripSpringTextBox から派生したクラスを提供します。 このクラスは、GetPreferredSize メソッドをオーバーライドして、他のすべての項目の合計幅が減算された後、親 ToolStrip コントロールの使用可能な幅を計算します。 このコード例では、新しい動作を示す Form クラスと Program クラスも提供します。

using System;
using System.Drawing;
using System.Windows.Forms;

public class ToolStripSpringTextBox : ToolStripTextBox
{
    public override Size GetPreferredSize(Size constrainingSize)
    {
        // Use the default size if the text box is on the overflow menu
        // or is on a vertical ToolStrip.
        if (IsOnOverflow || Owner.Orientation == Orientation.Vertical)
        {
            return DefaultSize;
        }

        // Declare a variable to store the total available width as
        // it is calculated, starting with the display width of the
        // owning ToolStrip.
        Int32 width = Owner.DisplayRectangle.Width;

        // Subtract the width of the overflow button if it is displayed.
        if (Owner.OverflowButton.Visible)
        {
            width = width - Owner.OverflowButton.Width -
                Owner.OverflowButton.Margin.Horizontal;
        }

        // Declare a variable to maintain a count of ToolStripSpringTextBox
        // items currently displayed in the owning ToolStrip.
        Int32 springBoxCount = 0;

        foreach (ToolStripItem item in Owner.Items)
        {
            // Ignore items on the overflow menu.
            if (item.IsOnOverflow) continue;

            if (item is ToolStripSpringTextBox)
            {
                // For ToolStripSpringTextBox items, increment the count and
                // subtract the margin width from the total available width.
                springBoxCount++;
                width -= item.Margin.Horizontal;
            }
            else
            {
                // For all other items, subtract the full width from the total
                // available width.
                width = width - item.Width - item.Margin.Horizontal;
            }
        }

        // If there are multiple ToolStripSpringTextBox items in the owning
        // ToolStrip, divide the total available width between them.
        if (springBoxCount > 1) width /= springBoxCount;

        // If the available width is less than the default width, use the
        // default width, forcing one or more items onto the overflow menu.
        if (width < DefaultSize.Width) width = DefaultSize.Width;

        // Retrieve the preferred size from the base class, but change the
        // width to the calculated width.
        Size size = base.GetPreferredSize(constrainingSize);
        size.Width = width;
        return size;
    }
}

public class Form1 : Form
{
    public Form1()
    {
        ToolStrip toolStrip1 = new ToolStrip();
        toolStrip1.Dock = DockStyle.Top;
        toolStrip1.Items.Add(new ToolStripLabel("Address"));
        toolStrip1.Items.Add(new ToolStripSpringTextBox());
        toolStrip1.Items.Add(new ToolStripButton("Go"));
        Controls.Add(toolStrip1);
        Text = "ToolStripSpringTextBox demo";
    }
}

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Public Class ToolStripSpringTextBox
    Inherits ToolStripTextBox

    Public Overrides Function GetPreferredSize( _
        ByVal constrainingSize As Size) As Size

        ' Use the default size if the text box is on the overflow menu
        ' or is on a vertical ToolStrip.
        If IsOnOverflow Or Owner.Orientation = Orientation.Vertical Then
            Return DefaultSize
        End If

        ' Declare a variable to store the total available width as 
        ' it is calculated, starting with the display width of the 
        ' owning ToolStrip.
        Dim width As Int32 = Owner.DisplayRectangle.Width

        ' Subtract the width of the overflow button if it is displayed. 
        If Owner.OverflowButton.Visible Then
            width = width - Owner.OverflowButton.Width - _
                Owner.OverflowButton.Margin.Horizontal()
        End If

        ' Declare a variable to maintain a count of ToolStripSpringTextBox 
        ' items currently displayed in the owning ToolStrip. 
        Dim springBoxCount As Int32 = 0

        For Each item As ToolStripItem In Owner.Items

            ' Ignore items on the overflow menu.
            If item.IsOnOverflow Then Continue For

            If TypeOf item Is ToolStripSpringTextBox Then
                ' For ToolStripSpringTextBox items, increment the count and 
                ' subtract the margin width from the total available width.
                springBoxCount += 1
                width -= item.Margin.Horizontal
            Else
                ' For all other items, subtract the full width from the total
                ' available width.
                width = width - item.Width - item.Margin.Horizontal
            End If
        Next

        ' If there are multiple ToolStripSpringTextBox items in the owning
        ' ToolStrip, divide the total available width between them. 
        If springBoxCount > 1 Then width = CInt(width / springBoxCount)

        ' If the available width is less than the default width, use the
        ' default width, forcing one or more items onto the overflow menu.
        If width < DefaultSize.Width Then width = DefaultSize.Width

        ' Retrieve the preferred size from the base class, but change the
        ' width to the calculated width. 
        Dim preferredSize As Size = MyBase.GetPreferredSize(constrainingSize)
        preferredSize.Width = width
        Return preferredSize

    End Function
End Class

Public Class Form1
    Inherits Form

    Public Sub New()
        Dim toolStrip1 As New ToolStrip()
        With toolStrip1
            .Dock = DockStyle.Top
            .Items.Add(New ToolStripLabel("Address"))
            .Items.Add(New ToolStripSpringTextBox())
            .Items.Add(New ToolStripButton("Go"))
        End With
        Controls.Add(toolStrip1)
        Text = "ToolStripSpringTextBox demo"
    End Sub

End Class

Public Class Program

    <STAThread()> Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1())
    End Sub

End Class

コードのコンパイル

この例では、次のものが必要です。

  • System、System.Drawing、および System.Windows.Forms アセンブリへの参照。

こちらも参照ください