最近做一个项目需要用到自定义控件,主要控件是一个TextBox。做好以后,在界面上体验极差。因为没做控件大小的考虑,所以在界面上这个自定义控件可以随便更改其大小,导致文本框不能填充整个自定义控件的区域。在网上找了好多资料,有一个说直接重写控件的Size属性,我照着试了一下,完全不能满足我的要求。又找了好久,功夫不负有心人,终于找到我想要的东西。现在整理一下,分享给大家。
首先要添加引用System.Design。然后在文件头添加
using System.Windows.Forms.Design;
新建一个控件设计类
public class MyTextDesigner:System.Windows.Forms.Design.ControlDesigner
{
public MyTextDesigner()
{
}
public override SelectionRules SelectionRules
{
get
{
SelectionRules rules = SelectionRules.Visible | SelectionRules.Moveable
| SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
return rules;
}
}
}