主表格概念?
就像 ASP.NET 中的母版页一样,我们对于 Windows 窗体应用程序是否也有类似的概念。 这样我就不必在整个应用程序中多次重复表单的相同部分(页眉和页脚)。
Like Master pages in ASP.NET, do we have any similar concept for Windows Form application. So that I dont have to repeat the same portion of form (Header and footer) many times through out the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个表单类来定义所需的组件,并使所有其他表单成为该表单类的子类。
这是 VB.NET 中的一个很好的示例。
Create a form class that defines the components you want, and make all your other forms a subclass of this form class.
Here is a good example in VB.NET.
这就是 Windows 窗体中 UserControl 的用途。
基本上,您将创建“主”表单,然后将使用“占位符”中的用户控件的类型来初始化主表单。 然后,表单将创建给定 Type 实例的控件,并将这些实例添加到 Controls 集合中。
That is what UserControls are for in Windows Forms.
Basically, you would create the "master" form and then the master form would be initialized with the types of the user controls that would go in the "placeholders". The form would just then create the controls given the Type instance(s) and add the instances to the Controls collection.
您可以创建一个基表单类,其中包含该类型的每个表单上的控件,然后从该基类派生所有实际表单。 这称为“视觉继承”,Windows 窗体设计器对此有一些很好的设计时支持。
或者,您也可以考虑使用一个带有页眉和页脚字段的实际表单以及一个用于放置您的个人“页面”的大空白面板。 将每个页面设为用户控件,并在运行时根据需要将其换入换出。 例如,这是制作“向导”对话框的标准方法。
You can create a base form class with the controls that will be on every form of that type, and then derive all of your actual forms from that base class. This is called "Visual Inheritance", and the Windows Forms designer has some nice design-time support for this.
Alternatively, you might also look into the notion of having one actual form with your header and footer fields and a big blank panel where your individual "pages" will go. Make each page a user control and swap them in and out as needed at run-time. This is a standard way of making "wizard" dialogs, for example.