2011-03-22 83 views
31

是否可以在FlowLayoutPanel的FlowLayoutPanel自动调整大小的FlowLayoutPanel中插入项目?这里有一个例子:FlowLayoutPanel - 控件的自动宽度?

1 FlowLayoutPanel的和3个按钮中的一种形式:

enter image description here

如果我调整窗体,控件的外观是这样的:他们安排“左到右”

enter image description here

我想是这样的:控制应该有FlowLayoutPanel的宽度:

enter image description here

任何想法如何做到这一点?我改变了FlowDirection和Anchor属性,但没有运气。

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

+3

是,调整大小正常工作。你做什么,做*不*增加500个控件,它会吸引严重的泥土。一个表格不应该有超过50个控件。 – 2011-03-22 18:09:31

+0

我想创建一种类似于Apple Automator中的“ListView”:http://bit.ly/fxkMaH – MilMike 2011-03-22 18:13:48

+0

我只在您提供的链接中看到4项,而不是500. – Justin 2011-03-22 20:01:29

回答

27

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

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

+1

但是,每次在其中添加控件时,都必须管理表格的行数,对吧? – J4N 2017-10-10 12:54:40

2

,我建议......尝试用按钮的锚玩....尝试将其设置为

Button1.Anchor = (AnchoreStyle.Left or AnchoreStyle.Right) 

或 设置它的属性...

,然后将其放在一个面板而不是FlowLayoutPanel ...;)

+0

同意。但是,Panel不能正确处理子控件边距。 – Larry 2013-01-18 18:32:41

13

这是简单的方法来做到这一点。 只需绑定您的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(); 
} 
+0

这是我遇到这个问题时的第一个想法,但不能减慢GUI? – Traubenfuchs 2014-05-20 13:40:59

+0

超级男人,它对我来说工作得很好。我将以上解决方案与这一个混合:) – Amir 2015-04-10 08:24:37

+1

太旧了,但我会建议使用'FlowLayoutPannel'的布局事件来调整控件的大小。 – bansi 2017-09-12 03:59:34

2

FlowLayoutPanel安排以特定的方式控制,根据MSDN

...垂直的流动方向,所述FlowLayoutPanel的控制计算 隐含柱从宽度 列中最宽的子控件。此列中的所有其他控件与锚点码头 属性已对齐或拉伸以适应此隐含列。对于水平流动方向, 行为的工作方式类似。

它不是理想的,但你可以原生做到这一点,只要一个子控件设置为相同的宽度作为容器,与对照组的其余部分设置为Dock

2

在这里,我有我的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; 
       } 
      } 
     } 
    } 
}