2012-12-08 44 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

相关问题