C# 如何 自动伸缩 ToolStripTextBox 拉伸 ToolStripTextBox 以填充 ToolStrip 的其余宽度(Windows 窗体)

示例

以下代码示例提供了一个派生自 ToolStripTextBox 的类,称为 ToolStripSpringTextBox。 此类替代 GetPreferredSize 方法,用于在减去所有其他项的组合宽度后计算父 ToolStrip 控件的可用宽度。 此代码示例还提供一个 Form 类和一个 Program 类来演示新行为。

如何:拉伸 ToolStripTextBox 以填充 ToolStrip 的其余宽度(Windows 窗体)

  • 项目
  • 2022/05/12
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());
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

功克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值