一个自定义控件,如何像TextBox控件一样,使其在设计时不能改变控件高度。
(TextBox的高度是一定的,高度改变的地方是灰掉的,不能用鼠标或者代码改变其高度。)

解决方案 »

  1.   

    你应该使用ReadOnlyAttribute:using System.ComponentModel;
    ...
    [ReadOnly(true)]
    public int Height
    {
     get...set...
    }或者去掉属性的set方法(如果不需要的话)
      

  2.   

    To: Sunmast(速马/Truly Madly Deeply) 
    这样好像不行哦!我说的可是Control.Size属性。
      

  3.   

    设计时限制改变大小
    如果是windows控件需要重载ControlDesigner的SelectionRules属性
    public override SelectionRules SelectionRules
    {
    get
    {
    SelectionRules rules1 = base.SelectionRules;
    rules1 = ((SelectionRules) ((int) rules1)) & ((SelectionRules) (-4));
    return rules1;
    }
    }
    上面是TextBox限制改变高度的实现但是如果你用代码还是可以改变他的高度的,所以如果做到真正的限制protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
    {
    height = 50;//这里为了简单直接定义高度为50
    //你可以检查属性等然后计算得出合适的高度
    base.SetBoundsCore (x, y, width, height, specified);
    }
      

  4.   

    最简单地,可以这样:
    const int _HEIGHT = 25;
    public new Size Size
    {
      get{return base.Size;}
      set{base.Size = new Size(value.Width,_HEIGHT);}
    }
    public new int Height
    {
      get{return _HEIGHT;}
    }
    这样无论设计器还是代码都没法改变其高度了如果要完全的灰掉设计器上关于高度的控制点,那么可以参考楼上的做法,设定SelectionRules
    不过这样你得自己写一个控件的Designer