重写C# TabControl添加方法
我想创建 TabControl 的自定义版本,以便在添加新 TabPage 时我可以确保执行一些自定义处理。
问题是如何重写 TabPages.Add() 方法来实现此目的?
谢谢, 理查德
I would like to create a custom version of the TabControl so that when a new TabPage is added I can ensure some custom processing is performed.
The question is how do I override the TabPages.Add() method to achieve this?
thanks,
Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,您无法重写
TabPageCollection
类的Add()
方法。您可以尝试的是订阅TabControl.ControlAdded
事件,希望当TabPage
(本质上是一个Control
作为好)将被添加。Unfortunatelly, you cannot override
Add()
method ofTabPageCollection
class. What you may try is to subscribe toTabControl.ControlAdded
event in hope that it will be raised when aTabPage
(which is essentially aControl
as well) will be added.您可以创建继承自 TabControl 的自定义版本,并具有
public new void Add(string)
方法。但如果有人将您的控件投射回 TabControl,他们就会绕过您的逻辑。您可以尝试创建一个继承自 System.Windows.Forms.Control 的自定义控件,并公开私有 TabControl 的所有方法,并根据需要修改 Add 方法。这会给你更多的控制权。You could create the custom version which inherits from the TabControl, and has a
public new void Add(string)
method. But if anyone casts your control back to the TabControl, they would go around your logic. You could try creating a custom control which inherits from System.Windows.Forms.Control and expose all the methods of a private TabControl, modifying the Add method as needed. This would give you much more control.