简体   繁体   中英

Cloning tabs in Tab Control on button click in Winforms

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");

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..

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

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.

  • Solution 1: Change the control modifiers to public as needed

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

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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