Windows 窗体中的主窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:16:56  来源:igfitidea点击:

Master Forms in windows form

windowswinformsforms

提问by Chetan

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.

与 ASP.NET 中的母版页一样,我们是否对 Windows 窗体应用程序有任何类似的概念。这样我就不必在整个应用程序中多次重复表单的相同部分(页眉和页脚)。

回答by Richard B

Yes.

是的。

What you have to do is to create your "Master" form, add in all the UI you want, the default buttons, etc.

您需要做的是创建您的“主”表单,添加您想要的所有 UI、默认按钮等。

For things like buttons, I typically create the methods that handle the Click events as "Virtual" so that I can implement them in the actual form that I'm then building.

对于按钮之类的东西,我通常会创建将 Click 事件处理为“虚拟”的方法,以便我可以在我正在构建的实际表单中实现它们。

Once the "Master" form is built, what you will need to do is to Right-Click on the project and select "New Item". Once you have done that, if you're in VS.Net 2008, you can click on the "Windows Forms" categories, and then select "Inherited Form".

构建“主”表单后,您需要做的是右键单击项目并选择“新建项目”。完成此操作后,如果您使用的是 VS.Net 2008,则可以单击“Windows 窗体”类别,然后选择“继承的窗体”。

Once you have done that, you will be asked to inherit from which form in the project. Simply select the "master" form, and you should be set.

完成后,您将被要求从项目中的哪个表单继承。只需选择“主”表单,您就应该设置好了。

回答by boj

Create the main form as a Form with header and footer, but leave the middle empty.

将主窗体创建为带有页眉和页脚的窗体,但将中间留空。

Implement "inner pages" as UserControlwith a common interface, and change them as you need it. (Header, Contentpanel and Footer are Windows.Form.Panel).

使用通用界面将“内页”作为UserControl实现,并根据需要更改它们。(页眉、内容面板和页脚是 Windows.Form.Panel)。

-------------------------------
| Header                      |
-------------------------------         ______________
|                             |         |            |
|         ContentPanel        |   <---- | MonkeyEdit |
|                             |         |____________|
-------------------------------
| Footer                      |
-------------------------------

And implement content as

并将内容实现为

public class MonkeyEdit : UserControl, IContent
{

}

Implementing an interface it's usefull but not neccessary. After that, based on events/configuration, just load MonkeyEditinto the ContentPanelwith a Dock.Fill. You can inhert the "master form" too, as you see here in an another post.

实现一个接口很有用但不是必需的。之后,根据事件/配置,只需使用Dock.Fill将 MonkeyEdit加载到ContentPanel 中。您也可以继承“主表单”,正如您在另一篇文章中看到的那样。

回答by Ed Guiness

Not really the same as master pages, but you can create a control (e.g based on UserControl) with header and footer that all your other forms can inherit (or construct) as needed.

与母版页并不完全相同,但您可以创建一个带有页眉和页脚的控件(例如基于UserControl),所有其他表单都可以根据需要继承(或构建)这些控件

回答by Rockcoder

You can achieve something similar with inheritance; you create some parent form with controls that are repeated and then use this parent for every custom form you need.

您可以通过继承实现类似的功能;您创建一些带有重复控件的父表单,然后将此父表单用于您需要的每个自定义表单。

回答by Geoff Appleford

Add a class the inherits from Form, add the headers and footers(in code) and then let all new forms inherit from this class.

添加一个继承自 Form 的类,添加页眉和页脚(在代码中),然后让所有新表单从这个类继承。

回答by Geoff Appleford

Master page has capability of specifying the content area where pages will be embedded. So deriving a form, say Form B from Form A and getting features of parent form doesn't prove the point. What Boj has mentioned makes more sense, where we are using Panels.

母版页具有指定将嵌入页面的内容区域的能力。因此,从表单 A 导出表单,例如从表单 B 获取表单并获取父表单的特征并不能证明这一点。Boj 提到的更有意义,我们在哪里使用面板。