C# Winform 多窗体切换方式一

  金牌会员 | 2024-8-12 10:36:20 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 507|帖子 507|积分 1521

一、简介

在 Winform 开辟中,多窗体的切换是一个常见的需求,比如登录成功后,切换至主界面,在网上查阅相关的资料,你会发现许多都是用 form2.Show();  this.Hide(); 这种方式,这种方式也存在一些问题,由于 Winform 存在一个主线程概念,如果主线程关闭,那么当前软件全部的窗体都会随之关闭,你也可以使用其他的方式,让界面继续表现,但整体上不是特别的优雅,这里保举两种方式,1.在 Form1 内里加载用户控件,并实时改变 Form1 界面巨细和用户控件保持同等,2.用一个窗体作为主线程启动,启动后隐藏界面,添加一个窗体管理类来实现其他的窗体的关闭和表现,如果末了一个表现的窗体关闭了,就关闭主线程退出程序,本篇文章我就使用第一种方式来实现结果。
结果:


二、实现结果

新建一个 Winform 项目,Form1 如下:

这里添加了背景颜色,主要作用是在后面切换用户控件时,观察是否严丝合缝。
添加了一个按钮,这个按钮用来切换用户控件。
另外,添加了三个用户控件:
1.Login

模仿登录界面

代码:
  1. using System;
  2. using System.Windows.Forms;
  3. namespace Winform多窗体切换
  4. {
  5.     public partial class Login : UserControl
  6.     {
  7.         public Login()
  8.         {
  9.             InitializeComponent();
  10.             this.ParentChanged += MyUserControl_ParentChanged;
  11.         }
  12.         private void Login_Load(object sender, EventArgs e)
  13.         {
  14.             //Console.WriteLine($"Login 宽度:{this.Size.Width},高度:{this.Size.Height}");
  15.             Console.WriteLine("登录界面 Login_Load 方法");
  16.         }
  17.         private void MyUserControl_ParentChanged(object sender, EventArgs e)
  18.         {
  19.             // 检查控件是否被移除
  20.             if (this.Parent == null)
  21.             {
  22.                 // 在这里添加你的清理代码,例如,释放托管资源,取消事件订阅等
  23.                 Console.WriteLine("登录界面 Disposed 方法");
  24.             }
  25.             else
  26.             {
  27.                 //在 Form1 添加当前用户控件时,这里会被执行
  28.                 Console.WriteLine("登录界面 Parent Changed: New Parent = " + this.Parent.Name);
  29.             }
  30.         }
  31.     }
  32. }
复制代码
由于用户控件没有 FormClosing 方法,这里订阅 ParentChanged 方法来实现这一结果,我试着订阅其他许多方法,都没有用果。

2.Setting

模仿设置界面

这里添加了四个按钮,用来判断用户控件是否表现完整
代码:
  1. using System;
  2. using System.Windows.Forms;
  3. namespace Winform多窗体切换
  4. {
  5.     public partial class Setting : UserControl
  6.     {
  7.         public Setting()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.         private void Setting_Load(object sender, EventArgs e)
  12.         {
  13.             //Console.WriteLine($"Setting 宽度:{this.Size.Width},高度:{this.Size.Height}");
  14.             Console.WriteLine("设置界面 Login_Load 方法");
  15.         }
  16.     }
  17. }
复制代码

3.Main

用来模仿主界面

代码:
  1. using System;
  2. using System.Windows.Forms;
  3. namespace Winform多窗体切换
  4. {
  5.     public partial class Main : UserControl
  6.     {
  7.         public Main()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.         private void Main_Load(object sender, EventArgs e)
  12.         {
  13.             //Console.WriteLine($"Main 宽度:{this.Size.Width},高度:{this.Size.Height}");
  14.             Console.WriteLine("Main 界面 Main_Load 方法");
  15.         }
  16.     }
  17. }
复制代码

Form1 代码:
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace Winform多窗体切换
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         private int Index = 0;
  13.         //高度页边距
  14.         private int WidthMargins = 16;
  15.         //高度页边距(无边框则为0)
  16.         private int HeightMargins = 39;
  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.         }
  20.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  21.         {
  22.         }
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             Index++;
  26.             //防止测试按钮被移除
  27.             foreach (Control c in this.Controls)
  28.             {
  29.                 if (c.Name != "button1")
  30.                     this.Controls.Remove(c);
  31.             }
  32.             if (Index == 1)
  33.             {
  34.                 Login login = new Login();
  35.                 this.Controls.Add(login);
  36.                 this.Size = new Size(login.Width + WidthMargins, login.Height + HeightMargins);
  37.             }
  38.             else if (Index == 2)
  39.             {
  40.                 Main main = new Main();
  41.                 this.Controls.Add(main);
  42.                 this.Size = new Size(main.Width + WidthMargins, main.Height + HeightMargins);
  43.             }
  44.             else if (Index == 3)
  45.             {
  46.                 Setting setting = new Setting();
  47.                 this.Controls.Add(setting);
  48.                 this.Size = new Size(setting.Width + WidthMargins, setting.Height + HeightMargins);
  49.             }
  50.             if (Index >= 3)
  51.                 Index = 0;
  52.         }
  53.     }
  54. }
复制代码
运行:

源码不必要积分和付费哦
https://download.csdn.net/download/qq_38693757/89627203

竣事

如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言
end

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表