2017-08-24 63 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(); 
相关问题