.NET Windows 窗体问题的自定义设计器类
我已经实现了一个继承自 DocumentDesigner 的自定义设计器类。 .NET Framework 中的标准 Form 类使用 FormDocumentDesigner 类(也继承自 DocumentDesigner),但由于此类是内部类,因此无法继承它并自定义其行为,我通过使用反射器复制了此类中的逻辑并插入它在我的自定义设计器类中(以便我的表单的默认设计时行为符合标准表单)。
一切工作正常,除了一件事:在我的自定义设计器类的 Initialize 方法中,我想插入一个 ToolStrip 控件,以便每次在设计时打开我的表单时,该控件都是可见且可编辑的。 问题在于,每次在设计器中关闭并重新打开窗体时都会调用 Initialize 方法,这会导致每次创建 ToolStrip 控件的新实例并将其添加到窗体的 Controls 集合中。 我正在寻找一种方法来检查代码中是否已经有一个 ToolStrip 控件序列化,并避免添加另一个控件。
直到现在我发现我可以使用IDesignerHost的LoadComplete事件并检查Form的Controls集合中是否有ToolStrip。 然而,任何更好的想法将不胜感激。
谢谢你的时间! :-)
I have implemented a custom designer class which inherits from DocumentDesigner. The standard Form class in the .NET Framework uses the FormDocumentDesigner class (also inheriting from DocumentDesigner) but since this class is internal it is not possible to inherit from it and customize its behavior I copied the logic in this class by using a reflector and inserted it in my custom designer class (so that the default design time behavior for my form is compliant with the standard Form).
Everything works fine except one thing: in the Initialize method of my custom designer class I want to insert a ToolStrip control so that each time my form is opened while in design time, this control is visible and editable. The problem is that the Initialize method is called each time you close and reopen the form in the designer which causes each time new instance of the ToolStrip control to be created and added in the Controls collection of the form. I am looking for a way to check whether there is already a ToolStrip control serialized in the code and avoid adding another one.
Until now I found out that I can use the LoadComplete event of the IDesignerHost and check whether there is a ToolStrip in the Controls collection of the Form. However, any better ideas would be appreciated.
Thanks for your time! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,LoadComplete 事件是正确的点。 我通常检查 Loading 属性来检查初始化是否在加载时发生,或者是否在加载后被调用。
另一种方法是创建一个可以放入表单中的组件,并为该组件创建一个设计器,而不是重新创建 FormDesigner。
从组件设计器中,您可以使用 _host.RootComponent 属性获取父表单。
Well, the LoadComplete event is the correct point. I usually check the Loading property to check if the Initialize is happening while loading or it has been called after the load.
Another approach is to create a Component that you can drop into a form and create a designer for the component instead of recreating the FormDesigner.
From the component designer you can get the parent form using the _host.RootComponent property.