C# Adding tabs at runtime using Form's controls

时光总嘲笑我的痴心妄想 提交于 2019-12-08 03:18:57

问题


I have thought of this idea where you could have a tab control on a form, but instead of defining each tabs controls before runtime, inherit or use another form's controls.

Basically a tab control is in place on the main form that has no tabs on it before runtime. When runtime comes along I want to create tabs, but the controls that would be on each tab would be from seperate already created forms.

Each form would be a seperate tab that had been created prior to runtime.

Is this possible? Is so, how?

Thanks in advance

EDIT I'm using 3.5


回答1:


first create a User Control and design it as you would a regular form, then add it to your TabControl.TabPages

TabPage page = new TabPage("Title");
page.Controls.Add(new CustomUserControl()); //your user control

this.tabControl1.TabPages.Add(page);



回答2:


If all you'll be doing is copying controls from a TabControl on one form to a TabControl on another form:

  1. Instantiate the form you want to copy from
  2. Find the TabControl on that form
  3. Iterate through Controls on the TabPages inside that TabControl
  4. Add each Control you find in that collection to the tab(s) in the control you want to copy to
  5. Close and Dispose of the form you created in step 1



回答3:


Yes, it is possible. You have to add the controls onto the TabPage, then add the TabPage to the TabControl.TabPages



来源:https://stackoverflow.com/questions/1155384/c-sharp-adding-tabs-at-runtime-using-forms-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!