A recent need to do a project, in order to avoid the trouble of choosing from the menu, need to integrate several forms together, through the TabControl tab to switch, so that the implementation of the design is to some extent the implementation of code reuse, The idea of extension is that we can put some small functions in the form to load in the container when needed, which also avoids the problem of frequent error when using the user control. This feature is similar to what you did before to load a form into a tab via the menu selection, different from the source of the excitation, let's take a look.
Project File organization:
Main Program Interface:
Write the tag of the two option pages in TabControl1 on the class names of Form2 and FORM3 respectively.
Included form Form2:
Included form FORM3:
Run results: From2 and FORM3 Two forms are integrated into the tabs in the Form1 display:
FORM1 Implementation code:
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.reflection;using system.text;using system.windows.forms;namespace tabWindowTest{ Public partial class Form1:form {public int[] s = {0, 0}; Used to record if the from has been opened public Form1 () {InitializeComponent (); The private void Form1_Load (object sender, EventArgs e) {//is loaded FORM2 string when it is initially opened FORMCLA SS = "Tabwindowtest.form2"; Generateform (Formclass, tabControl1); private void Tabcontrol1_selectedindexchanged (object sender, EventArgs e) {if (s[tabcontrol1.se lectedindex]==0)//Generate only once {Btnx_click (sender, E); }}////<summary>///General button click on tab to display the corresponding form on the tab///</summary> private void BT Nx_click (object sender, EventArgs e) {string FOrmclass = ((TabControl) sender). SelectedTab.Tag.ToString (); string form = TabControl1.SelectedTab.Tag.ToString (); Generateform (Formclass, sender); }//Generate form in tab public void Generateform (string form, object sender) {//Reflection build form Form fm = (form) assembly.getexecutingassembly (). CreateInstance (form); Set the form without borders to join the FM in the tab. FormBorderStyle = Formborderstyle.none; Fm. TopLevel = false; Fm. Parent = ((TabControl) sender). Selectedtab; Fm. ControlBox = false; Fm. Dock = DockStyle.Fill; Fm. Show (); s[((TabControl) sender). SelectedIndex] = 1; } }}