C # development WinForm How to integrate loading multiple forms in a tab implement form reuse

Source: Internet
Author: User
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; }    }}
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.