简体   繁体   English

C#中面板的Overrides Controls属性

[英]Overriding Controls property of a panel in C#

I have to override Add method of "Controls" property of myControl that is extended from a Panel control of windows. 我必须重写从Windows的Panel控件扩展的myControl的“ Controls”属性的Add方法。 For that i extended ControlCollection class into MyControlCollection where i overriden its Add method. 为此,我将ControlCollection类扩展到MyControlCollection中,在此我重写了其Add方法。 Now i declared a Controls property of MyControlCollection type to hide panel's Controls property. 现在,我声明了MyControlCollection类型的Controls属性以隐藏面板的Controls属性。 When i am accessing this.Controls.Add(control), it refers to overriden Add method. 当我访问this.Controls.Add(control)时,它指的是重写的Add方法。 But if i drags and drops a control on myControl the behaviour is of base type's Add method. 但是,如果我在myControl上拖放控件,则该行为属于基本类型的Add方法。 Can any body suggest the cause and remedy for this problem? 任何机构都可以提出该问题的原因并采取补救措施吗? Thanks in advance. 提前致谢。

You may instead override the CreateControlCollection function, and return an instance of the class of your choice, which inherits System.Web.UI.ControlCollection. 您可以替代重写CreateControlCollection函数,并返回您选择的类的实例,该类继承System.Web.UI.ControlCollection。 Remove the Controls property from your class, you should not need to override or hide original implementation. 从您的类中删除Controls属性,您不需要重写或隐藏原始实现。

The cause is that the designer is calling Control.Controls rather than accessing your separate collection. 原因是设计人员正在调用Control.Controls,而不是访问您的单独集合。 To be honest, your solution sounds like it's destined to cause trouble - hiding members usually does. 老实说,您的解决方案听起来注定会引起麻烦-隐藏成员通常会这样做。

What are you trying to achieve, exactly? 您到底想达到什么目的? It doesn't look like there's a nice event to hook into in ControlCollection but there may be a different way of tackling the problem. ControlCollection看起来好像没有一个不错的事件可以挂接,但是解决问题的方式可能有所不同。

EDIT: I've just seen that Control has a ControlAdded event - would subscribing to that be enough for you? 编辑:我刚刚看到Control有一个ControlAdded事件-订阅该内容就足够了吗?

Actually i have to restrict adding controls in my control if some flags are true. 实际上,如果某些标志为真,我必须限制在控件中添加控件。 I used that ControlAdded event but it added problems only. 我使用了ControlAdded事件,但它仅增加了问题。 ControlAdded fires only after adding the that control in parent at location 0, 0. After raising that event it relocates the control according to mouse position at droping time. 仅在将控件添加到位置0、0的父级中后,才触发ControlAdded。引发该事件后,控件将在放置时根据鼠标位置重新定位控件。 If i removes that new control from parent in ControlAdded's EventHandler exception occurs. 如果我在ControlAdded的EventHandler中从父级中删除了该新控件,则会发生。

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

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