上位机开发中,经常会需要将子窗体嵌入到主窗体。
运行结果
核心实现:
private void button2_Click(object sender, EventArgs e){Form3 childForm = new Form3();//判断容器中是否已经打开子窗体,如果打开现将其关闭foreach (Control item in this.splitContainer1.Panel2.Controls){if (item is Form){((Form)item).Close();Console.WriteLine("找到以前的窗体并关闭");}}childForm.TopLevel = false;//将子窗体设置成非顶级控件childForm.FormBorderStyle = FormBorderStyle.None;//如果是带边框的窗体,需要去掉窗体边框;childForm.Parent = this.splitContainer1.Panel2;childForm.Dock = DockStyle.Fill;//设置随着容器的大小自动调整窗体大小;childForm.Show();}
代码提炼优化为通用方法
public void OpenForm(Form childForm)//按钮事件调用通用方法
{if (childForm == null){return;}//Form2 childForm = new Form2();//判断容器中是否已经打开子窗体,如果打开现将其关闭foreach (Control item in this.splitContainer1.Panel2.Controls){if (item is Form){((Form)item).Close();Console.WriteLine("找到以前的窗体并关闭");}}childForm.TopLevel = false;//将子窗体设置成非顶级控件childForm.FormBorderStyle = FormBorderStyle.None;//如果是带边框的窗体,需要去掉窗体边框;childForm.Parent = this.splitContainer1.Panel2;childForm.Dock = DockStyle.Fill;//设置随着容器的大小自动调整窗体大小;childForm.Show();
}
调用方法
private void button3_Click(object sender, EventArgs e)
{OpenForm(new Form2());
}
完整
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){Form2 childForm = new Form2();//判断容器中是否已经打开子窗体,如果打开现将其关闭foreach (Control item in this.splitContainer1.Panel2.Controls){if (item is Form){((Form)item).Close();Console.WriteLine("找到以前的窗体并关闭");}}childForm.TopLevel = false;//将子窗体设置成非顶级控件childForm.FormBorderStyle = FormBorderStyle.None;//如果是带边框的窗体,需要去掉窗体边框;childForm.Parent = this.splitContainer1.Panel2;childForm.Dock = DockStyle.Fill;//设置随着容器的大小自动调整窗体大小;childForm.Show();}private void button2_Click(object sender, EventArgs e){Form3 childForm = new Form3();//判断容器中是否已经打开子窗体,如果打开现将其关闭foreach (Control item in this.splitContainer1.Panel2.Controls){if (item is Form){((Form)item).Close();Console.WriteLine("找到以前的窗体并关闭");}}childForm.TopLevel = false;//将子窗体设置成非顶级控件childForm.FormBorderStyle = FormBorderStyle.None;//如果是带边框的窗体,需要去掉窗体边框;childForm.Parent = this.splitContainer1.Panel2;childForm.Dock = DockStyle.Fill;//设置随着容器的大小自动调整窗体大小;childForm.Show();}public void OpenForm(Form childForm)//按钮事件调用通用方法{if (childForm == null){return;}//Form2 childForm = new Form2();//判断容器中是否已经打开子窗体,如果打开现将其关闭foreach (Control item in this.splitContainer1.Panel2.Controls){if (item is Form){((Form)item).Close();Console.WriteLine("找到以前的窗体并关闭");}}childForm.TopLevel = false;//将子窗体设置成非顶级控件childForm.FormBorderStyle = FormBorderStyle.None;//如果是带边框的窗体,需要去掉窗体边框;childForm.Parent = this.splitContainer1.Panel2;childForm.Dock = DockStyle.Fill;//设置随着容器的大小自动调整窗体大小;childForm.Show();}private void button3_Click(object sender, EventArgs e){OpenForm(new Form2());}private void button4_Click(object sender, EventArgs e){OpenForm(new Form3());}private void button5_Click(object sender, EventArgs e){Form2 fm2 = new Form2();OpenForm(fm2);}private void button6_Click(object sender, EventArgs e){Form3 fm3 = new Form3();OpenForm(fm3);}}
}
代码下载:
【免费】C#中在主窗体的某个容器中打开子窗体的方法资源-CSDN文库https://download.csdn.net/download/anlog/89363912
参考链接:
C#知识|上位机子窗体嵌入主窗体方法(实例) (qq.com)https://mp.weixin.qq.com/s?__biz=MzU4MzgyMDEzOA==&mid=2247486750&idx=1&sn=c6eb7c358c4034c03bb9e768aef70413&chksm=fda27b1bcad5f20d15abae895e50b336366bf58fe0a0350126cbaf3409350bac7845407e1bef&mpshare=1&scene=1&srcid=05262aK0RkJdJaBcUxjNEd5Z&sharer_shareinfo=214d0d5572caabea801daa11554170a3&sharer_shareinfo_first=5c9166718e80955786a42b0207e83e1f#rd
C# WinForm:无法访问已释放的对象 - asdyzh - 博客园 (cnblogs.com)https://www.cnblogs.com/asdyzh/p/9902520.html
特此记录
anlog
2024年5月27日