C # How does WinForm integrate and load multiple forms in the tab?
To avoid the trouble of selecting from the menu, you need to integrate several forms and switch through the TabControl option card. This function is similar to loading a form to a tab through menu selection. The difference is that the source of the excitation is different. Let's take a look.
Project file organization:
Main program interface:
Write the form2 and form3 class names for the tags on the two option pages in tabControl1 respectively.
Included form Form2:
Included form Form3:
Running result: the from2 and form3 forms are integrated into the form1 tab:
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 whether public Form1 () is enabled from () {InitializeComponent ();} private void formateload (object sender, EventArgs e) {// load Form2 string formClass = "tabWindowTest. form2 "GenerateForm (formClass, tabControl1);} private void tabControl1_SelectedIndexChanged (object sender, EventArgs e) {if (s [tabControl1.SelectedIndex] = 0) // generate only {btnX_Click (sender, e );}}////// Click the common button to display the corresponding form on the tab ///Private void btnX_Click (object sender, EventArgs e) {string formClass = (TabControl) sender ). selectedTab. tag. toString (); // string form = tabControl1.SelectedTab. tag. toString (); GenerateForm (formClass, sender);} // generate the form public void GenerateForm (string form, object sender) on the tab) {// Form fm = (Form) Assembly generated by reflection. getExecutingAssembly (). createInstance (form); // set that the form has no border and adds it to the tab fm. 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 ;}}}