Home > Backend Development > C#.Net Tutorial > C# development How to integrate and load multiple forms in WinForm tabs to achieve form reuse Detailed explanation (picture)

C# development How to integrate and load multiple forms in WinForm tabs to achieve form reuse Detailed explanation (picture)

黄舟
Release: 2017-03-10 14:10:59
Original
3152 people have browsed it

A project I need to do recently, in order to avoid the trouble of selecting from the menu, several forms need to be integrated together and switched through the TabControl tab. This design implementation also achieves code duplication to a certain extent. Using and extending the idea, we can put some small functions in the form and load them in the container when needed. This can also avoid the problem of frequent errors when using user controls. This function is similar to the previous one of loading a form into a tab through menu selection. The difference is that the source of stimulation is different. Let's take a look.

Project file organization:

Main program interface:

Place tabControl1 in Write the class names of form2 and form3 on the tags of the two option pages respectively.

Contained form Form2:

Contained form Form3:

Run results: both forms from2 and form3 are integrated into the tab in form1 and displayed:

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};         //用来记录from是否打开过

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //初始打开时就加载Form2
            string formClass = "tabWindowTest.Form2";
            GenerateForm(formClass, tabControl1);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(s[tabControl1.SelectedIndex]==0)    //只生成一次
            {
                btnX_Click(sender, e);
            }
        }

        /// <summary>
        /// 通用按钮点击选项卡 在选项卡上显示对应的窗体
        /// </summary>
        private void btnX_Click(object sender, EventArgs e)
        {
            string formClass = ((TabControl)sender).SelectedTab.Tag.ToString();

            //string form = tabControl1.SelectedTab.Tag.ToString();

            GenerateForm(formClass, sender);

        }


        //在选项卡中生成窗体
        public void GenerateForm(string form, object sender)
        {
            // 反射生成窗体
            Form fm = (Form)Assembly.GetExecutingAssembly().CreateInstance(form);

            //设置窗体没有边框 加入到选项卡中
              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;

        }
    }
}
Copy after login



The above is the detailed content of C# development How to integrate and load multiple forms in WinForm tabs to achieve form reuse Detailed explanation (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template