信息网站大全,电子商务网站建设 百度文库,wordpress 链接修改密码,百度爱采购怎么优化排名一、简介
在 Winform 开发中#xff0c;多窗体的切换是一个常见的需求#xff0c;比如登录成功后#xff0c;切换至主界面#xff0c;在网上查阅相关的资料#xff0c;你会发现很多都是用 form2.Show(); this.Hide(); 这种方式#xff0c;这种方式也存在一些问题#…
一、简介
在 Winform 开发中多窗体的切换是一个常见的需求比如登录成功后切换至主界面在网上查阅相关的资料你会发现很多都是用 form2.Show(); this.Hide(); 这种方式这种方式也存在一些问题因为 Winform 存在一个主线程概念如果主线程关闭那么当前软件所有的窗体都会随之关闭你也可以使用其他的方式让界面继续显示但整体上不是特别的优雅这里推荐两种方式1.在 Form1 里面加载用户控件并实时改变 Form1 界面大小和用户控件保持一致2.用一个窗体作为主线程启动启动后隐藏界面添加一个窗体管理类来实现其他的窗体的关闭和显示如果最后一个显示的窗体关闭了就关闭主线程退出程序本篇文章我就使用第一种方式来实现效果。
效果 二、实现效果
新建一个 Winform 项目Form1 如下 这里添加了背景颜色主要作用是在后面切换用户控件时观察是否严丝合缝。
添加了一个按钮这个按钮用来切换用户控件。
另外添加了三个用户控件
1.Login
模拟登录界面 代码
using System;
using System.Windows.Forms;namespace Winform多窗体切换
{public partial class Login : UserControl{public Login(){InitializeComponent();this.ParentChanged MyUserControl_ParentChanged;}private void Login_Load(object sender, EventArgs e){//Console.WriteLine($Login 宽度{this.Size.Width}高度{this.Size.Height});Console.WriteLine(登录界面 Login_Load 方法);}private void MyUserControl_ParentChanged(object sender, EventArgs e){// 检查控件是否被移除if (this.Parent null){// 在这里添加你的清理代码例如释放托管资源取消事件订阅等Console.WriteLine(登录界面 Disposed 方法);}else{//在 Form1 添加当前用户控件时这里会被执行Console.WriteLine(登录界面 Parent Changed: New Parent this.Parent.Name);}}}
}
由于用户控件没有 FormClosing 方法这里订阅 ParentChanged 方法来实现这一效果我试着订阅其他很多方法都没有效果。 2.Setting
模拟设置界面 这里添加了四个按钮用来判断用户控件是否显示完整
代码
using System;
using System.Windows.Forms;namespace Winform多窗体切换
{public partial class Setting : UserControl{public Setting(){InitializeComponent();}private void Setting_Load(object sender, EventArgs e){//Console.WriteLine($Setting 宽度{this.Size.Width}高度{this.Size.Height});Console.WriteLine(设置界面 Login_Load 方法);}}
} 3.Main
用来模拟主界面 代码
using System;
using System.Windows.Forms;namespace Winform多窗体切换
{public partial class Main : UserControl{public Main(){InitializeComponent();}private void Main_Load(object sender, EventArgs e){//Console.WriteLine($Main 宽度{this.Size.Width}高度{this.Size.Height});Console.WriteLine(Main 界面 Main_Load 方法);}}
} Form1 代码
using System;
using System.Drawing;
using System.Windows.Forms;namespace Winform多窗体切换
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private int Index 0;//高度页边距private int WidthMargins 16;//高度页边距无边框则为0private int HeightMargins 39;private void Form1_Load(object sender, EventArgs e){}private void Form1_FormClosing(object sender, FormClosingEventArgs e){}private void button1_Click(object sender, EventArgs e){Index;//防止测试按钮被移除foreach (Control c in this.Controls){if (c.Name ! button1)this.Controls.Remove(c);}if (Index 1){Login login new Login();this.Controls.Add(login);this.Size new Size(login.Width WidthMargins, login.Height HeightMargins);}else if (Index 2){Main main new Main();this.Controls.Add(main);this.Size new Size(main.Width WidthMargins, main.Height HeightMargins);}else if (Index 3){Setting setting new Setting();this.Controls.Add(setting);this.Size new Size(setting.Width WidthMargins, setting.Height HeightMargins);}if (Index 3)Index 0;}}
}
运行 源码不需要积分和付费哦
https://download.csdn.net/download/qq_38693757/89627203 结束
如果这个帖子对你有所帮助欢迎 关注 点赞 留言
end