做一个类似于TextBox一样在设计时只能调整位置和宽度,不能修改它的高度,请使用下面的方法:
假定我的控件名称为PopupCheckedList,高度只能是21像素。
首先,需要与控件对应的一个Desinger类,名为<PopupCheckedListDesigner>,代码如下:
public class PopupCheckedListDesigner : System.Windows.Forms.Design.ControlDesigner { public override System.Windows.Forms.Design.SelectionRules SelectionRules { get { return System.Windows.Forms.Design.SelectionRules.LeftSizeable | System.Windows.Forms.Design.SelectionRules.RightSizeable | System.Windows.Forms.Design.SelectionRules.Moveable; } } }
这里面的SelectionRules就是控制了设计时可以执行的选中操作,定义如下,大家一看就明白了:
// Summary: // Defines identifiers that are used to indicate selection rules for a component. [Flags] public enum SelectionRules { // Summary: // Indicates the component is locked to its container. Overrides the System.Windows.Forms.Design.SelectionRules.Moveable, // System.Windows.Forms.Design.SelectionRules.AllSizeable, System.Windows.Forms.Design.SelectionRules.BottomSizeable, // System.Windows.Forms.Design.SelectionRules.LeftSizeable, System.Windows.Forms.Design.SelectionRules.RightSizeable, // and System.Windows.Forms.Design.SelectionRules.TopSizeable bit flags of this // enumeration. Locked = -2147483648, // // Summary: // Indicates no special selection attributes. None = 0, // // Summary: // Indicates the component supports resize from the top. TopSizeable = 1, // // Summary: // Indicates the component supports resize from the bottom. BottomSizeable = 2, // // Summary: // Indicates the component supports resize from the left. LeftSizeable = 4, // // Summary: // Indicates the component supports resize from the right. RightSizeable = 8, // // Summary: // Indicates the component supports sizing in all directions. AllSizeable = 15, // // Summary: // Indicates the component supports a location property that allows it to be // moved on the screen. Moveable = 268435456, // // Summary: // Indicates the component has some form of visible user interface and the selection // service is drawing a selection border around this user interface. If a selected // component has this rule set, you can assume that the component implements // System.ComponentModel.IComponent and that it is associated with a corresponding // designer instance. Visible = 1073741824, }
第二步,对控件类应用特性标记,标识它的Designer类。
[DesignerAttribute(typeof(PopupCheckedListDesigner))] public partial class PopupCheckedList : DevExpress.XtraEditors.XtraUserControl
第三部,在构造函数中,将高度修改为默认的21.
public PopupCheckedList() { InitializeComponent(); base.Height = popupContainerEdit1.Height; base.Width = 150; this.checkedListBoxControl1.ItemCheck += new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(checkedListBoxControl1_ItemCheck); }
注意,一定要在InitializeComponent函数调用之后。
这样已经OK了,我们看看在设计时,控件的选中状态吧。
这个控件在选中时,就没有可以上下拖动的锚点,一看就让使用着知道高度是不可更改的。
当然手工代码还是可以修改这个控件的高度,比如修改Control.height属性,那怎么办哪?
你就需要额外的写点东西了,第一覆盖常用的Height属性和Size属性,使用自定义的代码逻辑,禁止外部修改它的高度。
比如下面的代码段,去掉了Height的Set方法。
public new int Height { get { return base.Height; } }
同样,还需要在Resize事件中进行判断,如果高度不是21的话,就强制修改为21,以防止其他方式修改了控件的Height。