


C# development How to integrate and load multiple forms in WinForm tabs to achieve form reuse Detailed explanation (picture)
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; } } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.
