C # How does WinForm integrate and load multiple forms in the tab?

Source: Internet
Author: User

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 ;}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.