2011-03-22 87 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; 
       } 
      } 
     } 
    } 
}