【问题标题】:child control larger than parent when width equal宽度相等时子控件大于父控件
【发布时间】:2012-12-09 04:53:36
【问题描述】:

我正在尝试调整自定义面板控件的大小,使其在宽度上适合父容器。当我检查控件值时,它们都是相同的,但是子控件对于父控件来说太宽了。

造成这种差异的原因是什么?我希望能够计算出正确的尺寸。我尝试更改填充和边距选项,但这没有任何效果。

[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;
            }
    }
}

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    使用表单和控件的ClientSize

    control.ClientSize.Width
    

    form.Widthcontrol.Width 是包括边框的外部宽度。 ClientRectangle 是可以放置其他控件的边框之间的内容,ClientSize 是这个内部矩形的大小。

    【讨论】:

      猜你喜欢
      • 2023-01-03
      • 2013-07-14
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2015-10-25
      相关资源
      最近更新 更多