简体   繁体   English

跨多个标签页重新使用控件(即文本框)

[英]Re-Using controls (ie Textboxes) across multiple tab pages

I have a C# WinForm program that is essentially a glorified text parser. 我有一个C#WinForm程序,它实质上是一个美化的文本解析器。 It takes a line of hexidecimal and converts it to human readable information based on the specs that were presented to me. 它需要一行十六进制,并根据提供给我的规格将其转换为人类可读的信息。 I have a tabcontrol with several tab pages used to display the information. 我有一个tabcontrol,其中有几个用于显示信息的标签页。 Several of these tabpages are identical but need to be on different tabs based on the line of text parsed out, if the line is of type 'A' it displays the information on tab 'A', if it is of type 'B', it shows on tab 'B' and so on. 这些标签页中的几个标签页是相同的,但根据所解析的文本行,它们必须位于不同的标签页上;如果该行的类型为“ A”,则在标签页“ A”上显示该信息;如果该行的类型为“ B”,它显示在标签“ B”上,依此类推。

The problem I am starting to notice with this, is I have quite a bit of textboxes and other controls spanned over all of these tab pages and I believe it is effecting performance a little. 我开始注意到的问题是,我在所有这些选项卡页面上都有大量的文本框和其他控件,并且我相信它会影响性能。 I beleive I have in the range of 150+ textboxes now, about the same checkboxes and the like. 我相信我现在拥有150多个文本框,大致相同的复选框等。

The question I have is, is there a way to reuse one page of controls over multiple tab pages and have them display different information based on the tab, so perhaps I can cut down my 100's of controls and simplify the program itself? 我的问题是,是否可以在多个选项卡页面上重用一页控件,并让它们根据选项卡显示不同的信息,所以也许我可以减少100个控件并简化程序本身?

Read down to the added section for a bit more clarity, as I rambled a bit. 在我漫步时,请仔细阅读添加的部分,以使内容更加清晰。

There are a couple of ways to solve this architecturally. 有两种方法可以在体系结构上解决此问题。 the basic concept is stop thinking about "tab pages" as a physical concept and start thinking about it logically. 基本概念是停止将“标签页”视为一个物理概念,而开始从逻辑上进行思考。 As a logical concept, in your implementation, a tab page is a set of controls, some of which are unique and some of which are common (or common to more than one tab). 从逻辑上讲,在您的实现中,标签页是一组控件,其中一些控件是唯一的,而某些控件是公用的(或多个标签所共有)。

In a purely dynamic system, you fill in the "controls" and dynamically paint depending on which "tab" the person clicks. 在纯动态系统中,您可以填写“控件”并根据用户单击的“选项卡”动态绘制。 This means you do not really need to use a tab control. 这意味着您实际上不需要使用选项卡控件。 Instead you paint the "tab page" based on what information should display to the user. 而是根据应向用户显示的信息来绘制“选项卡页”。

Another option is to leave the unique "controls" already laid out on a "tab page" and leave containers you can add in the non-unique controls to. 另一种选择是保留已经在“选项卡页”上布置的唯一“控件”,并保留可以添加到非唯一控件中的容器。 This takes a bit of planning to determine what controls need to display on what page and how the layout of static (controls placed on a tab) and dynamic intertwine. 这需要一些计划,以确定哪些控件需要显示在哪个页面上以及静态(控件放置在选项卡上)和动态交错的布局。 In other words, a bit of planning can save you a lot of grief. 换句话说,稍作计划可以为您省去很多麻烦。

Either way, you want to fill a single control once, no matter how many "tab pages" it is used on. 无论哪种方式,您都希望一次填充一个控件,而不管它使用了多少“标签页”。

Added: Since this is winforms, you have to change your thought paradigm a bit. 补充:由于这是winforms,因此您必须稍微改变一下思维模式。 If you were a webforms developer, you would already think in terms of containers. 如果您是Webforms开发人员,那么您已经在容器方面进行了思考。

One option is a strategy pattern for tabs. 一个选项是选项卡的策略模式。 the strategy contains the controls needed to paint the "tab page" and their location and size on the page. 该策略包含绘制“标签页”所需的控件及其在页面上的位置和大小。 When a person clicks a tab, you reach into the "bucket o controls" and dynamically add based on the "tab" (strategy) used. 当某人单击一个选项卡时,您可以进入“ bucket o控件”并根据所使用的“选项卡”(策略)动态添加。 You can even pass in the tab page to the strategy and have it dynamically painted there. 您甚至可以将选项卡页面传递给策略,并在其中动态绘制策略。

the main point in the entire answer is do the work once and then display as needed. 整个答案的重点是一次完成工作,然后根据需要显示。

我认为您不能像这样重复使用控件,但是您可以在第一次显示选项卡时创建控件,而并​​非一次全部创建。

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

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