当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 如何禁止调整自定义控件的尺寸?

ASP.NET
在图片上加入图片版权信息
Peer-to-Peer (P2P) communication across middleboxes(翻译4)
今天完成了.net compact framework 加 web service的演练.
Cordbg, Dumpbin, Ildasm, 的一些教程。
asp和asp.net的session共用
VB连接SQL数据库的模块
消除图片在ie中缓存而无法更新的问题
说说使用static和const关键字
怎样解决thephile中的数据库由于排序造成的问题:对 text 数据类型不支持代码页转...
.net分布式事务例子
Internet Explorer 编程简述(二)
使用SqlParameter参数返回值时遇到的问题
vb可不可以实现虚拟中断
C#下Socket对象的BeginReceive方法,执行后竟然不调用AsyncCallback里的回调函数
坚持学asp.net:(十一)
[C#][正则表达式]寻找匹配的Groups的几种方法
面向服务的体系结构概述
Windows Form 和 UserControl
VB中類模塊實現與C++中類實現的比較(1)
下载Oracle数据库中的Blob二进制文件,实例!

ASP.NET 中的 如何禁止调整自定义控件的尺寸?


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 106 ::
收藏到网摘: n/a

有时我们在自定义控件时,出于某种原因的考虑(比如:防止在设计时误操作),想禁止调整自定义控件的尺寸(Height 或 Width)。最初我是这样实现的,这也是较简单的方法:public class MyButton : System.Windows.Forms.Button{... ... protected override void OnResize(EventArgs e) { this.Height = 23; this.Width = 75; }}  但是我对这样的效果不太满意,要是能实现像TextBox那样,在设计时上下边缘的小方块是灰的,而左右边缘的小方块是白的(表示无法调整其Height),那该有多酷!经过一番研究和到CSDN上求助,终于解决了此问题,效果如下图所示: 现在将代码整理出来,希望能对大家有所帮助。 1、建立自定义控件设计器类。/// /// 自定义控件设计器类/// public class MyButtonDesigner : System.Windows.Forms.Design.ControlDesigner{ public MyButtonDesigner() { } public override SelectionRules SelectionRules { get {//不允许调整控件的高度,具体说明详见MSDN。 SelectionRules rules = SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable; return rules; } }} 2、给自定义控件类添加属性,将该控件类与上面定义的设计器类关联起来。[Designer(typeof(MyButtonDesigner))]public class MyButton : System.Windows.Forms.Button{... ...} 经过以上处理,就实现了上述效果。不过如果再仔细研究一下,你会发现这只在设计时有效,而在运行时,还是能够改变该控件的高度。如何避免这个问题呢?请在相应位置加入以下代码(如有不清楚的地方请查阅MSDN)。public class MyButton : System.Windows.Forms.Button{... ... protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) { base.SetBoundsCore(x, y, width, 23, specified); }} 最后提醒大家一下:要成功完成编译,必须添加System.Design引用,并在文件头部加上:using System.Windows.Forms.Design;