在按钮上的“选项卡控件"中克隆选项卡,然后在Winforms中单击 [英] Cloning tabs in Tab Control on button click in Winforms

查看:41
本文介绍了在按钮上的“选项卡控件"中克隆选项卡,然后在Winforms中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在winform TabControl的其中一个TabPages上有一个主页.我有一个按钮,它使用此 Tabs.TabPages.Add("Homepage");

so I have a Homepage on one of the TabPages on winform TabControl. I have a button, that adds a new tab using this Tabs.TabPages.Add("Homepage");

但是TabPages.Add()仅添加一个新的空白页,我如何在新选项卡上克隆主页?例如,如果我的主页上有一个按钮"Click me",则当我打开一个新标签时,我希望它具有与相同事件"ClickMe_click"链接的相同按钮"Click me".就像Chrome Tab控件一样.除非我错过了,否则我找不到msdn上TabControl内置的任何事件或方法.

But TabPages.Add() just adds a new blank page, how would i clone my HomePage on the new tab? For instance, if my homepage has a button "Click me", when i open a new tab, I want it to have the same button "Click me" linked to the same event "ClickMe_click". Like a Chrome Tab control. I couldn't find any event or method built in for TabControl on msdn, unless i missed it.

任何帮助,提示或建议将不胜感激.谢谢.

Any help or hint or suggestion would be appreciated. Thanks.

推荐答案

您无法如此轻松地克隆标签页.一个人可以尝试,但是推荐的方法是:

You can't clone a tabpage so easily. One can try but the recommended way is:

  • 创建一个

  • Create a UserControl and add all the controls you want on your page. Make it Dock=Fill and add any code that connects the controls. You can layout as freely as you would in a form or a tabpage..

每当您需要这种类型的另一个页面时,请添加一个新的选项卡页,并将 UserControl 的实例添加到其 Controls 集合中.

Whenever you want another page of this type, add a new tabpage and add an instance of the UserControl to its Controls collection.

注意:默认情况下, UserControl 上的所有控件均为 private .这与将它们直接放在选项卡页上并没有什么不同.但是现在它们是UC类的私有成员,因此您的表单及其代码无法访问它们.

Note: All controls on your UserControl by default are private. This is not really different from placing them on the tabpage directly. But now they are private members of the UC class, so your form and its code can't access them.

初次接触时看起来像个问题.但是,如果您看对的话,这是创建精简工具的好机会.界面更干净.

Looks like a problem when you're new to it. But if you look at it right, it is a good oportunity to create a leaner & cleaner interface.

  • 解决方案1:根据需要将控件修饰符更改为 public

解决方案2:添加属性以暴露那些您真正想要暴露的数据.

Solution 2: Add properties to expose those data you really want to expose.

还请注意:您可以进行所有布局,但只能在UC设计器中进行.添加到表单或标签页后,将没有嵌入式设计器.

Also note: You can do all layout but can do so only in the UC designer. After adding to a form or tabpage there will be no embedded designer..

这篇关于在按钮上的“选项卡控件"中克隆选项卡,然后在Winforms中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆