简体   繁体   English

阻止表单的表演事件;

[英]Blocking Show Event Of A Form;

我正在尝试编写一个自定义对话框。一切都很好,但我不希望我的Dialog可以通过Show()事件调用。是否可以阻止它?

If your class inherits from Form it's not possible. 如果您的班级是从Form继承的,那是不可能的。 Show is not virtual, so you can't override it. Show不是虚拟的,因此您无法覆盖它。 You should shadow it, and have it throw an exception or call ShowDialog internally (there is no way to remove it as an option entirely), but then any caller that just types the variable as a Form can still call Show , bypassing your implementation. 您应该对其进行阴影处理,并使其引发异常或在内部调用ShowDialog (无法完全将其作为选项删除),但是任何仅将变量键入为Form调用方仍可以绕过您的实现来调用Show

The only option available to you would be for your class to not inherit from Form . 唯一可用的选择是使您的类不从Form继承。 You'd need to design your type to use composition rather than inheritance. 您需要设计类型以使用组合而不是继承。 Have a Form property within your type's definition, and manipulate it, add controls to it, etc. as you might normally manipulate the type itself. 在类型的定义中包含一个Form属性,然后对其进行操作,向其中添加控件等,就像通常可以操作类型本身一样。

You would need to explicitly create members for whatever functionality that you want to expose externally, redirecting the appropriate calls to the encapsulated form (for methods such as ShowDialog ). 您需要为要在外部公开的任何功能显式创建成员,将适当的调用重定向到封装的表单(对于诸如ShowDialog方法)。

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

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