FlowLayoutPanel - 控件的自动宽度?

Mil*_*ike 39 .net vb.net resize flowlayoutpanel

是否可以使FlowLayoutPanel中的插入项自动调整为FlowLayoutPanel?这是一个例子:

一个包含1个FlowLayoutPanel和3个按钮的表单:

在此输入图像描述

如果我调整表单大小,控件看起来像这样:他们安排"从左到右"

在此输入图像描述

我想要的是:控件应该具有FlowLayoutPanel的宽度:

在此输入图像描述

任何想法如何做到这一点?我更改了FlowDirection并使用了Anchor属性,但没有运气.

我当然可以调整FlowLayoutPanel_Resize事件中的控件,但我想添加大约500个用户控件 - 我测试了它并且速度很慢.

arb*_*ter 39

我建议你在这种情况下使用TableLayoutPanel和一列.我发现TableLayoutPanel比FlowLayoutPanel更可预测和更可靠.

如果您仍想使用FlowLayoutPanel,另一个选项是将第一个控件宽度设置为所需的宽度,并将Dock = Top用于所有其他控件.

  • 但是,每次您在其中添加控件时,都必须管理表的行数,对吗? (2认同)

Sup*_*cky 19

这是一种简单的方法.只需绑定flowLayoutPannel的SizeChanged evnent并调整包含控件的大小.喜欢:

private void myFlowLayoutPannel_SizeChanged(object sender, EventArgs e)
{
    myFlowLayoutPannel.SuspendLayout();
    foreach (Control ctrl in pnSMS.Controls)
    {
        if (ctrl is Button) ctrl.Width = pnSMS.ClientSize.Width;
    }
    myFlowLayoutPannel.ResumeLayout();
}
Run Code Online (Sandbox Code Playgroud)

  • 太旧了,但我建议使用`FlowLayoutPannel`的`Layout`事件来调整控件的大小. (3认同)

Gra*_*ate 7

这里不需要FlowLayoutPanel

您应该能够使用正常Panel控件做您想做的事情。将其固定在所有四个边,以便它随着您的形状延伸,然后添加按钮并将它们全部设置为“Dock: Top”。

编辑-回应@UsamaAziz 评论。

为了确保隐藏在面板底部之外的控件可以访问,请将面板的“AutoScroll”属性设置为 True。这将在需要时向面板添加垂直滚动条。

任务完成。


Teo*_*zza 6

这里我有我的StackPanel类:

/// <summary>
/// A stackpanel similar to the Wpf stackpanel.
/// </summary>
public class StackPanel: FlowLayoutPanel
{
    public StackPanel(): base()
    {
        InitializeComponent();
        this.ForceAutoresizeOfControls = true;
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        //
        // StackPanel
        //
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.WrapContents = false;
        this.ResumeLayout(false);
    }

    /// <summary>
    /// Override it just in order to hide it in design mode.
    /// </summary>
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new bool WrapContents
    {
        get { return base.WrapContents; }
        set { base.WrapContents = value; }
    }

    /// <summary>
    /// Override it just in order to set its default value.
    /// </summary>
    [DefaultValue(typeof(AutoSizeMode), "GrowAndShrink")]
    public override AutoSizeMode AutoSizeMode
    {
        get { return base.AutoSizeMode; }
        set { base.AutoSizeMode = value; }
    }

    /// <summary>
    /// Get or set a value that when is true forces the resizing of each control.
    /// If this value is false then only control that have AutoSize == true will be resized to
    /// fit the client size of this container.
    /// </summary>
    [DefaultValue(true)]
    public bool ForceAutoresizeOfControls { get; set; }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        this.SuspendLayout();
        switch (FlowDirection)
        {
            case FlowDirection.BottomUp:
            case FlowDirection.TopDown:
                foreach (Control control in this.Controls)
                    if (ForceAutoresizeOfControls || control.AutoSize)
                        control.Width = this.ClientSize.Width - control.Margin.Left - control.Margin.Right;
                break;
            case FlowDirection.LeftToRight:
            case FlowDirection.RightToLeft:
                foreach (Control control in this.Controls)
                    if (ForceAutoresizeOfControls || control.AutoSize)
                        control.Height = this.ClientSize.Height - control.Margin.Top - control.Margin.Bottom;
                break;
            default:
                break;
        }
        this.ResumeLayout();
    }

    protected override void OnLayout(LayoutEventArgs levent)
    {
        base.OnLayout(levent);

        if (levent != null && levent.AffectedControl != null)
        {
            Control control = levent.AffectedControl;
            if (ForceAutoresizeOfControls || control.AutoSize)
            {
                switch (FlowDirection)
                {
                    case FlowDirection.BottomUp:
                    case FlowDirection.TopDown:
                        control.Width = this.ClientSize.Width - control.Margin.Left - control.Margin.Right;
                        break;
                    case FlowDirection.LeftToRight:
                    case FlowDirection.RightToLeft:
                        control.Height = this.ClientSize.Height - control.Margin.Top - control.Margin.Bottom;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)