简体   繁体   English

C#标签控件-从TabPage继承吗?

[英]C# Tab Control - Inherit from TabPage?

Basically, I am creating a Windows Form with a Tab Control on it, and I want to have multiple types of tab pages for the different types of objects I will be editing the properties of. 基本上,我正在创建一个带有选项卡控件的Windows窗体,并且我想为要编辑其属性的不同类型的对象提供多种类型的选项卡页。 Basically like interface in Visual Studio, where you can have multiple tab pages of code, then multiple tabs of resource files, etc. 基本上类似于Visual Studio中的界面,您可以在其中具有多个标签页的代码,然后具有资源文件的多个标签等。

To do this, I figure that I will need to: 为此,我认为我需要:

  1. Create a class inheriting from TabPage for each type of tabpage layout I would like. 为我想要的每种类型的Tabpage布局创建一个从TabPage继承的类。
  2. Make each of these classes accept a certain type of object in the constructor. 使这些类中的每个类在构造函数中接受某种类型的对象。
  3. In the constructor, create all the controls for that tab page and set the properties. 在构造函数中,为该选项卡页面创建所有控件并设置属性。

However, this means that I cannot design the tab pages in Visual Studio (I will need to code the layout by hand). 但是,这意味着我无法在Visual Studio中设计选项卡页(我将需要手工编写布局代码)。

Am I going about this the correct way? 我要用正确的方法吗? Or is there a different way to approach this problem? 还是有解决此问题的其他方法?

Have you considered creating a Custom Control for each type of page that you want to display? 您是否考虑过为要显示的每种页面类型创建自定义控件? Rather than having different types of tabs, you can then use Visual Studio to design the layout of each custom control (along with any code that is per-tab), and when a new tab is created, make its sole child control an instance of the relevant type of custom control. 然后,您可以使用Visual Studio设计每个自定义控件的布局(以及每个选项卡的所有代码),而不是使用不同类型的选项卡,并且在创建新选项卡时,使其唯一的子控件成为相关类型的自定义控件。

Advantages : 优点

  • This allows you to use VS to design the look and feel of the tab contents 这使您可以使用VS设计选项卡内容的外观
  • If you want to re-use the UI for the contents of the tab elsewhere, it's already decoupled from the tab control (this would even help if at a later date you moved to using windows instead of tabs, for example). 如果您想在其他地方重新使用UI用于选项卡的内容,则它已经与选项卡控件分离了(例如,如果以后您转向使用Windows而不是选项卡,这甚至会有所帮助)。

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

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