2012-12-08 107 views
0

我試圖調整自定義面板控件的大小,使其適合父容器的寬度。當我檢查控件值時,它們都是相同的,但子控件對於父控件來說太寬了。當寬度相等時子控件大於父元素

什麼是造成差異?我想能夠計算出正確的尺寸。我試着改變填充和邊距選項,但是這沒有任何效果。

Extends too far to the right

[Category("Collapsible Panel")] 
[DesignOnly(true)] 
[DefaultValue(false)] 
[Description("If True, fits the panel to match the parent width")] 
public bool FitToParent 
{ 
    get { return _fitToParent; } 
    set { 
     _fitToParent = value; 
      if (_fitToParent) 
      { 
       if (this.Parent != null) 
       { 
        this.Location = new Point(0, this.Location.Y); 
        this.Size = new Size(this.Parent.Size.Width, this.Size.Height); 
        this.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; 
       } 
      } 
      else 
      { 
       this.Anchor = AnchorStyles.Top | AnchorStyles.Left; 
      } 
    } 
} 

回答

2

用的窗體和控件的ClientSize工作。

control.ClientSize.Width 

form.Widthcontrol.Width是包括邊框的外部寬度。 ClientRectangle是可以放置其他控件的邊界和ClientSize是此內部矩形的大小之間的區域。

+0

+1看起來就是這樣的問題 –

+0

邦上,謝謝:) – Amicable

相關問題