I have to override Add method of "Controls" property of myControl that is extended from a Panel control of windows. For that i extended ControlCollection class into MyControlCollection where i overriden its Add method. Now i declared a Controls property of MyControlCollection type to hide panel's Controls property. When i am accessing this.Controls.Add(control), it refers to overriden Add method. But if i drags and drops a control on myControl the behaviour is of base type's Add method. Can any body suggest the cause and remedy for this problem? Thanks in advance.
我必须覆盖从Windows的Panel控件扩展的myControl的“Controls”属性的Add方法。为此,我将ControlCollection类扩展为MyControlCollection,其中我覆盖了其Add方法。现在我声明了MyControlCollection类型的Controls属性来隐藏面板的Controls属性。当我访问this.Controls.Add(控件)时,它指的是覆盖Add方法。但是如果我在myControl上拖放控件,行为就是基类型的Add方法。任何人都可以提出这个问题的原因和解决办法吗?提前致谢。
3 个解决方案
#1
3
You may instead override the CreateControlCollection function, and return an instance of the class of your choice, which inherits System.Web.UI.ControlCollection. Remove the Controls property from your class, you should not need to override or hide original implementation.
您可以改为覆盖CreateControlCollection函数,并返回您选择的类的实例,该实例继承System.Web.UI.ControlCollection。从类中删除Controls属性,您不需要覆盖或隐藏原始实现。
#2
1
The cause is that the designer is calling Control.Controls rather than accessing your separate collection. To be honest, your solution sounds like it's destined to cause trouble - hiding members usually does.
原因是设计者正在调用Control.Controls而不是访问您的单独集合。说实话,你的解决方案听起来似乎注定要引起麻烦 - 隐藏成员通常会这样做。
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事件 - 订阅这个就足够了吗?
#3
0
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 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. If i removes that new control from parent in ControlAdded's EventHandler exception occurs.
实际上,如果某些标志为真,我必须限制在我的控件中添加控件。我使用了ControlAdded事件,但它只添加了问题。只有在位置0,0处的父级中添加该控件后,ControlAdded才会触发。在引发该事件后,它会根据鼠标位置在丢弃时重新定位控件。如果我在ControlAdded的EventHandler异常中删除了父控件的新控件。
#1
3
You may instead override the CreateControlCollection function, and return an instance of the class of your choice, which inherits System.Web.UI.ControlCollection. Remove the Controls property from your class, you should not need to override or hide original implementation.
您可以改为覆盖CreateControlCollection函数,并返回您选择的类的实例,该实例继承System.Web.UI.ControlCollection。从类中删除Controls属性,您不需要覆盖或隐藏原始实现。
#2
1
The cause is that the designer is calling Control.Controls rather than accessing your separate collection. To be honest, your solution sounds like it's destined to cause trouble - hiding members usually does.
原因是设计者正在调用Control.Controls而不是访问您的单独集合。说实话,你的解决方案听起来似乎注定要引起麻烦 - 隐藏成员通常会这样做。
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事件 - 订阅这个就足够了吗?
#3
0
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 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. If i removes that new control from parent in ControlAdded's EventHandler exception occurs.
实际上,如果某些标志为真,我必须限制在我的控件中添加控件。我使用了ControlAdded事件,但它只添加了问题。只有在位置0,0处的父级中添加该控件后,ControlAdded才会触发。在引发该事件后,它会根据鼠标位置在丢弃时重新定位控件。如果我在ControlAdded的EventHandler异常中删除了父控件的新控件。