2013-03-25 28 views
12

我有一個FlowLayoutPanel問題,我不知道如何解決它。如何讓FlowLayoutPanel.AutoSize與FlowBreak配合使用

我把兩個FlowLayoutPanels放在另一個裏面;第二個內部flp裏面有3個按鈕。

enter image description here

從FlowLayoutPanel的孩子的特性是:

FlowDirection = LeftToRight; 
AutoSize = true; 
AutoSizeMode = GrowAndShrink; 
WrapContents = true; 

現在我爲每個按鈕設置FlowBreak屬性爲true,但是我看到的行爲是不是我想要的,我想要的FlowLayoutPanel的收縮到按鈕的寬度,

enter image description here

更改FlowDirectionUpToDown不是一個選項。

任何人都知道爲什麼AutoSize不起作用?

這是代碼。

// 
//FlowLayoutPanel1 
// 
this.FlowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 
this.FlowLayoutPanel1.Controls.Add(this.FlowLayoutPanel3); 
this.FlowLayoutPanel1.Location = new System.Drawing.Point(84, 77); 
this.FlowLayoutPanel1.MinimumSize = new System.Drawing.Size(10, 10); 
this.FlowLayoutPanel1.Name = "FlowLayoutPanel1"; 
this.FlowLayoutPanel1.Size = new System.Drawing.Size(308, 265); 
this.FlowLayoutPanel1.TabIndex = 0; 
// 
//FlowLayoutPanel3 
// 
this.FlowLayoutPanel3.AutoSize = true; 
this.FlowLayoutPanel3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; 
this.FlowLayoutPanel3.Controls.Add(this.Button1); 
this.FlowLayoutPanel3.Controls.Add(this.Button2); 
this.FlowLayoutPanel3.Controls.Add(this.Button3); 
this.FlowLayoutPanel3.Location = new System.Drawing.Point(127, 3); 
this.FlowLayoutPanel3.MinimumSize = new System.Drawing.Size(10, 10); 
this.FlowLayoutPanel3.Name = "FlowLayoutPanel3"; 
this.FlowLayoutPanel3.Size = new System.Drawing.Size(162, 87); 
this.FlowLayoutPanel3.TabIndex = 1; 
// 
//Button1 
// 
this.FlowLayoutPanel3.SetFlowBreak(this.Button1, true); 
this.Button1.Location = new System.Drawing.Point(3, 3); 
this.Button1.Name = "Button1"; 
this.Button1.Size = new System.Drawing.Size(75, 23); 
this.Button1.TabIndex = 0; 
this.Button1.Text = "Button1"; 
this.Button1.UseVisualStyleBackColor = true; 
// 
//Button2 
// 
this.FlowLayoutPanel3.SetFlowBreak(this.Button2, true); 
this.Button2.Location = new System.Drawing.Point(3, 32); 
this.Button2.Name = "Button2"; 
this.Button2.Size = new System.Drawing.Size(75, 23); 
this.Button2.TabIndex = 1; 
this.Button2.Text = "Button2"; 
this.Button2.UseVisualStyleBackColor = true; 
// 
//Button3 
// 
this.Button3.Location = new System.Drawing.Point(3, 61); 
this.Button3.Name = "Button3"; 
this.Button3.Size = new System.Drawing.Size(75, 23); 
this.Button3.TabIndex = 2; 
this.Button3.Text = "Button3"; 
this.Button3.UseVisualStyleBackColor = true; 
+0

這是我的GUI問題 一個控制 https://docs.google.com/document/d/1I6OtboresNk-gfOR3sEM8gyYHVX9sGohdNtX6heeQwI/edit?usp=sharing 當我把2所控制,並設置FlowBrake到True https://docs.google.com/document/d/17C02PoL8LCyymfXNtEP8N6kkzZETxkiOCb6mPTbzGf0/edit?usp = sharing 我希望這些控件保持不變,但寬度變化不適合。 – Natalia 2013-03-25 20:30:39

+0

您可以添加兩個圖像 - 一個與你有什麼和另一個與你試圖實現。從文字描述中不清楚你面臨的問題 – 2013-04-03 13:28:41

+0

我添加了一個新的圖像和結果,在我以前的評論中,我把兩個鏈接與我的GUI上的行爲 – Natalia 2013-04-03 14:28:08

回答

13

這是一個錯誤,它已經存在很長時間了。問題是FlowLayoutPanel的佈局引擎計算第一行的寬度錯誤,包括第二個控件的寬度,即使它被換到第二行。

解決方法很愚蠢但很有效,在第一個控件後添加一個寬度爲0的虛擬面板。如果您正在與設計人員一起完成此操作,請先放下它,然後將其拖放到第一個控件右側的正確位置。然後在「屬性」窗口中將其邊距設置爲(0,0,0,0),並將大小設置爲(0,0)。

+0

保存我的一天..謝謝! – vendettamit 2017-05-31 20:45:13

2

不是解決方法,而是一種解決方法。看起來您正試圖通過在FlowLayoutPanel中使用流量中斷來模擬TableLayoutPanel的行爲。你試過用TableLayoutPanel代替嗎?根據評論中的截圖,它應該完美地滿足您的需求。

+0

在我的gui中,用戶可以安排任何按鈕的順序和位置。用戶也可以將按鈕拖入和拖出容器。如果我創建了表格佈局面板,那麼當用戶放棄對此的控制時,表格的行爲並不是微不足道的,因爲我們可以有一列控件或一行或一個表格。 – Natalia 2013-04-03 15:04:20

+0

@Natalia:你是否試圖實現[this](http://www.devexpress.com/Products/NET/Controls/WinForms/Layout/)? – Neolisk 2013-04-03 15:05:55

6

我不相信FlowLayoutPanel的設計是爲了做你想做的事情。 TableLayoutPanel可能更適合。用一列添加一個TableLayoutPanel,並將每個按鈕添加到一行中。

編輯:我發現了一個駭人的工作。第一個按鈕後,創建一個面板大小爲0,0,邊距爲0,0。確保FlowBreak設置爲false。

enter image description here

編輯:你只需要創建一個面板,第一個按鈕後,一個不爲每個。

+1

同樣的想法,同一時間。 Upvoting ... :) – Neolisk 2013-04-03 14:58:53

+0

@Michael G我會試試這個,等一下幾分鐘... – Natalia 2013-04-03 15:23:22

+1

你的'hackish work around'很棒。我試圖找到這樣的一個,但面板控制沒有出現在我的腦海。我希望我可以給你另一個upvote。 – Neolisk 2013-04-03 15:36:21

相關問題