繁体   English   中英

设置UserControl的最大高度而不设置最大宽度

[英]Set UserControl's max height without setting max width

我有一个带有预定义值的ComboBox的UserControl。 我认为使用此控件的程序员将能够水平扩展控件,而不能垂直扩展控件。

在这里@ SLaks建议设置MaximumSize属性,但是, 相比之下 ,我必须限制宽度,这是我不想设置的。

那么如何实现呢?

您可以重写SetBoundsCore并使它使用ComboBox高度作为控件的高度:

protected override void SetBoundsCore(int x, int y, int width, int height,
    BoundsSpecified specified)
{
    base.SetBoundsCore(x, y, width, comboBox1.Height, specified);
}

注意1:如果您的UserControl仅包含ComboBox ,则最好从ComboBox派生而不是创建包含ComboBox的用户控件

注意2:在设计器中,您可以通过创建新的ControlDesigner并覆盖SelectionRules属性,使控件仅能从左或向右调整大小。 为此,添加对System.Design程序集的引用,然后创建一个自定义设计器:

using System.Windows.Forms.Design;
public class MyControlDesigner : ControlDesigner
{
    public override SelectionRules SelectionRules
    {
        get
        {
            return SelectionRules.LeftSizeable |
                   SelectionRules.RightSizeable |
                   SelectionRules.Moveable;
        }
    }
}

然后就可以通过以下方式用designer属性装饰您的自定义控件:

[Designer(typeof(MyControlDesigner))]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM