2012-09-13 113 views

回答

1

你可以很容易地創建你自己定製的FlowLayoutPanel的由面板獲得。

儘管下面的代碼爲水平和垂直設置了一個統一的邊框,但您可以很容易修改每個屬性的水平和垂直間距。

public class FlowPanel : Panel 
{ 
    protected int _TileBorder; 
    public int TileBorder 
    { 
     get 
     { 
      return this._TileBorder; 
     } 
     set 
     { 
      this._TileBorder = value; 
      this.RearrangeControls(this, this.TileBorder); 
     } 
    } 
    public FlowPanel() 
    { 
     this.AutoScroll = true; 
    } 
    public FlowPanel(int tileBorder) 
    { 
     this.AutoScroll = true; 
     this.TileBorder = tileBorder; 
    } 
    protected override void OnSizeChanged(EventArgs e) 
    { 
     base.OnSizeChanged(e); 
     this.RearrangeControls(this, this.TileBorder); 
    } 
    protected override void OnControlAdded(ControlEventArgs e) 
    { 
     base.OnControlAdded(e); 
     this.RearrangeControls(this, this.TileBorder); 
     e.Control.SizeChanged += new EventHandler(this.Control_SizeChanged); 
    } 
    protected override void OnControlRemoved(ControlEventArgs e) 
    { 
     base.OnControlRemoved(e); 
     this.RearrangeControls(this, this.TileBorder); 
     e.Control.SizeChanged -= new EventHandler(this.Control_SizeChanged); 
    } 
    protected void RearrangeControls(Panel p, int border) 
    { 
     int num = border; 
     int num2 = border; 
     int num3 = 0; 
     bool flag = true; 
     foreach (Control control in p.Controls) 
     { 
      if (control != null) 
      { 
       num3 = ((control.Height > num3) ? control.Height : num3); 
       if (flag) 
       { 
        num += control.Width + border; 
        control.Location = new Point(border, border); 
        flag = false; 
       } 
       else 
       { 
        if (num + control.Width + 2 * border > p.Width) 
        { 
         num2 += num3 + border; 
         control.Location = new Point(border, num2); 
         num = border + (control.Width + border); 
         num3 = control.Height; 
        } 
        else 
        { 
         control.Location = new Point(num, num2); 
         num += control.Width + border; 
        } 
       } 
      } 
     } 
    } 
    private void Control_SizeChanged(object sender, EventArgs e) 
    { 
     this.RearrangeControls(this, this.TileBorder); 
    } 
} 

現在,我看它時,RearrangeControls方法並略有砍死在一起-IN-A-急於現在看我,但我敢肯定,你可以讓它更好!

+0

問題是設置了默認高度的內部面板。 – Malcolm