首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FlowLayoutPanel -控件的自动宽度?

FlowLayoutPanel -控件的自动宽度?
EN

Stack Overflow用户
提问于 2011-03-23 02:01:10
回答 7查看 44.2K关注 0票数 49

是否可以使FlowLayoutPanel中插入的项目自动调整FlowLayoutPanel的大小?下面是一个示例:

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

如果我调整窗体的大小,控件看起来像这样:它们“从左到右”排列。

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

你知道该怎么做吗?我更改了FlowDirection并使用了Anchor属性,但没有成功。

我当然可以在FlowLayoutPanel_Resize事件中调整控件的大小,但我想添加大约500个用户控件-我测试了它,它很慢。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-03-23 04:40:23

在这种情况下,我建议您使用具有一个列的TableLayoutPanel。我发现TableLayoutPanel比FlowLayoutPanel更可预测、更可靠。

如果您仍然想使用FlowLayoutPanel,另一个选择是将第一个控件宽度设置为所需的宽度,然后对所有其他控件使用Dock = Top。

票数 48
EN

Stack Overflow用户

发布于 2013-03-21 00:27:45

这是一种简单的方法。只需绑定flowLayoutPannel的SizeChanged事件并调整包含控件的大小即可。像这样:

代码语言:javascript
复制
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();
}
票数 20
EN

Stack Overflow用户

发布于 2017-04-12 16:16:57

下面是我的StackPanel类:

代码语言:javascript
复制
/// <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;
                }
            }
        }
    }
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5395754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档