2017-08-24 65 views
0

問題您如何使AutoSize立即生效?

我動態地將按鈕添加到WinForm。正如我這樣做,我重新定位現有的按鈕,以防止重疊。正在使用AutoSize屬性自動設置Width

對於更長的文本(將按鈕超出默認值Width),下面的代碼不起作用。

例如:

  • b.Width是75 AutoSize之前被設置
  • b.Width是75 AutoSize被設定後
  • 當換檔以外的其他鍵時,它通過b.Width + buffer = 83
  • 然而後對其進行移位addButton()完成,AutoSize將寬度設置爲150,重疊下一個僅83像素的Button 158

Here is a picture demonstrating the result

AutoSize出現改變控件的大小來不及爲它是使用。我怎樣才能立即做到這一點?

嘗試1 - 代碼

public void addButton(string text) 
{ 
    const int buffer = 8; 

    //Construct new button 
    Button b = new Button(); 
    b.Text = text; 
    b.AutoSize = true; 
    b.Location = new Point(0, 0); 

    //Shift over all other buttons to prevent overlap 
    //b.Width is incorrect below, because b.AutoSize hasn't taken effect 
    for (int i = 0; i < Controls.Count; i++) 
     if (Controls[i] is Button) 
      Controls[i].Location = new Point(Controls[i].Location.X + b.Width + buffer, Controls[i].Location.Y); 

    Controls.add(b); 
} 

嘗試2

搜查谷歌和StackOverflow的以下:

  • C#自動調整立即
  • C#自動調整快捷
  • C#自動調整工作不

嘗試3

這裏問。

最後手段

如果沒有別的辦法,定時器可以設置爲重新定位在每個tick按鈕。然而這是非常草率的設計,並沒有幫助學習AutoSize的錯綜複雜。如果可能,我想避免這種解決方法。

+0

我不知道這是最好的選擇(我還沒有找到一個更好的解決方案),但我做了什麼在過去,通過在其中一個事件處理程序上放置一個事件處理程序並在Resize上更改它們的位置來重新定位我所有的自動大小按鈕。 –

+0

我在[Button.AutoResize設置後直接獲取寬度]回答了這個問題(https://stackoverflow.com/questions/45784751/get-width-directly-after-button-autoresize-is-set/45786796#45786796) – TnTinMn

+0

@TnTinMn如果您以前回答過相同的問題,那麼您應該投票將其作爲重複關閉。 –

回答

1

AutoSizeAutoSizeMode模式僅當控制父到另一個控制或形式施用。

所以調用第一

Controls.Add(b); 

現在b.Size將相應調整,並且可以在計算中使用。

另一方面,不是Size屬性,你可以使用GetPreferredSize方法以獲得正確的尺寸,而不實際應用AutoSize,並用它計算裏面:

var bSize = b.GetPreferredSize(Size.Empty); 

//Shift over all other buttons to prevent overlap 
//b.Width is incorrect below, because b.AutoSize hasn't taken effect 
for (int i = 0; i < Controls.Count; i++) 
    if (Controls[i] is Button) 
     Controls[i].Location = new Point(Controls[i].Location.X + bSize.Width + buffer, Controls[i].Location.Y); 
0

您可以訂閱添加的最後一個按鈕的Resize事件。這將允許您準確地更改所有按鈕的位置,因爲現在所有按鈕都已自動調整大小。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     var button1 = NewButton(0); 
     button1.Location = new Point(10, 10); 

     var button2 = NewButton(1); 
     button2.Location = new Point(button1.Right, 10); 

     var button3 = NewButton(2); 
     button3.Location = new Point(button2.Right, 10); 

     button3.Resize += (s, e) => 
     { 
      button2.Location = new Point(button1.Right, 10); 
      button3.Location = new Point(button2.Right, 10); 
     }; 

     Controls.Add(button1); 
     Controls.Add(button2); 
     Controls.Add(button3); 
    } 

    private Button NewButton(int index) 
    { 
     return new Button() 
     { 
      Text = "ButtonButtonButton" + index.ToString(), 
      AutoSize = true 
     }; 
    } 
} 
1

FlowLayoutPanel控制這是否對你的工作。

將一個窗體上,並嘗試添加按鈕以下列方式:

Button b = new Button(); 
b.AutoSize = true; 
b.Text = text; 
flowLayoutPanel1.SuspendLayout(); 
flowLayoutPanel1.Controls.Add(b); 
flowLayoutPanel1.Controls.SetChildIndex(b, 0); 
flowLayoutPanel1.ResumeLayout(); 
相關問題