網站首頁 編程語言 正文
我上次編寫winform程序時發生了ObjectDisposedException,后來使用了try catch代碼塊還是沒有解決,源代碼:
using System.Windows.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
/// <summary>
/// 應用程序的主入口點
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm()); //在這里,視覺樣式是默認的樣式,以系統的默認樣式為基準
}
}
public class MainForm : Form
{
public MainForm()
{
FormPanel fp = new FormPanel(20);
fp.Location = new Point(0, 0);
}
}
public class FormPanel : UserControl
{
public Panel Head = new Panel();
public Label Close_Label = new Label();
public FormPanel(int Head_Height)
{
#region Head
Head.Parent = this;
Head.Dock = DockStyle.Top;
Head.Height = Head_Height;
#endregion Head
Form f1 = new Form();
this.Parent = f1;
#region Close_Label
Close_Label.Parent = Head;
Close_Label.Dock = DockStyle.Right;
Close_Label.AutoSize = true;
Close_Label.Text = "x"; //這里的x不是x,為了美觀,輸入了全角字符,不要隨便使用
Close_Label.MouseClick += (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
f1.Close();
this.Dispose();
}
}
#endregion
}
}
endregion后面可以添加任何字符,編譯后相當于不存在,也就是單行注釋,但不能刪除,因為有region
加上try catch代碼塊以后:
?using System.Windows.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
/// <summary>
/// 應用程序的主入口點
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm()); //在這里,視覺樣式是默認的樣式,以系統的默認樣式為基準
}
}
public class MainForm : Form
{
public MainForm()
{
FormPanel fp = new FormPanel(20);
fp.Location = new Point(0, 0);
}
}
public class FormPanel : UserControl
{
public Panel Head = new Panel();
public Label Close_Label = new Label();
public FormPanel(int Head_Height)
{
#region Head
Head.Parent = this;
Head.Dock = DockStyle.Top;
Head.Height = Head_Height;
#endregion Head
Form f1 = new Form();
this.Parent = f1;
#region Close_Label
Close_Label.Parent = Head;
Close_Label.Dock = DockStyle.Right;
Close_Label.AutoSize = true;
Close_Label.Text = "x"; //這里的x不是x,為了美觀,輸入了全角字符,不要隨便使用
Close_Label.MouseClick += (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
try
{
f1.Close();
this.Dispose();
}
catch { }
}
}
#endregion
}
}
原因是:
f1.Close()導致執行了FormPanel.Dispose(),這樣try catch代碼塊不在class外面,解決方法:
public class FormPanel : UserControl
{
public void Close(IsDispose)
{
this.Parent = null;
f1.Close();
if (IsDispose)
this.Dispose();
}
Form f1 = new Form(); //將f1設為全局變量
public Panel Head = new Panel();
public Label Close_Label = new Label();
public FormPanel(int Head_Height, bool CloseIsDispose)
{
#region Head
Head.Parent = this;
Head.Dock = DockStyle.Top;
Head.Height = Head_Height;
#endregion Head
this.Parent = f1;
#region Close_Label
Close_Label.Parent = Head;
Close_Label.Dock = DockStyle.Right;
Close_Label.AutoSize = true;
Close_Label.Text = "x"; //這里的x不是x,為了美觀,輸入了全角字符,不要隨便使用
Close_Label.MouseClick += (sender, e) =>
{
if (e.Button == MouseButtons.Left)
{
this.Close(CloseIsDispose);
}
}
#endregion
}
}
總結:盡量不要手動添加代碼關閉這個控件類的父窗體,如要關閉父窗體,請確認Close函數要在所有代碼之后,否則設置這個控件類的父容器為null,再關閉父窗體,執行完所有代碼之后,清除這個控件類。
原文鏈接:https://blog.csdn.net/weixin_72719643/article/details/128512008
相關推薦
- 2022-03-27 Linux上搭載Nginx負載均衡配置使用案例詳解_nginx
- 2023-11-20 Linux/樹莓派如何限制CPU使用率?cpulimit的基本用法
- 2023-01-23 redis實現多級緩存同步方案詳解_Redis
- 2023-05-23 Numpy數組轉置的實現_python
- 2022-07-16 idea 編寫springmvc項目并部署到Tomcat
- 2023-03-27 Python中反轉二維數組的行和列問題_python
- 2022-10-23 Android?Handler源碼深入探究_Android
- 2023-12-24 mybatis-plus代碼生成器使用(generator)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支