C# try catch代碼塊不起效果的解決方法
我上次編寫winform程序時(shí)發(fā)生了ObjectDisposedException,后來使用了try catch代碼塊還是沒有解決,源代碼:
using System.Windows.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm()); //在這里,視覺樣式是默認(rèn)的樣式,以系統(tǒng)的默認(rèn)樣式為基準(zhǔn)
}
}
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后面可以添加任何字符,編譯后相當(dāng)于不存在,也就是單行注釋,但不能刪除,因?yàn)橛衦egion
加上try catch代碼塊以后:
?using System.Windows.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm()); //在這里,視覺樣式是默認(rèn)的樣式,以系統(tǒng)的默認(rèn)樣式為基準(zhǔn)
}
}
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()導(dǎo)致執(zhí)行了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設(shè)為全局變量
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
}
}總結(jié):盡量不要手動添加代碼關(guān)閉這個(gè)控件類的父窗體,如要關(guān)閉父窗體,請確認(rèn)Close函數(shù)要在所有代碼之后,否則設(shè)置這個(gè)控件類的父容器為null,再關(guān)閉父窗體,執(zhí)行完所有代碼之后,清除這個(gè)控件類。
到此這篇關(guān)于C# try catch代碼塊不起效果的解決方法的文章就介紹到這了,更多相關(guān)C# try catch代碼塊不起效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#獲取客戶端相關(guān)信息實(shí)例總結(jié)
這篇文章主要介紹了C#獲取客戶端相關(guān)信息的方法,以實(shí)例形式總結(jié)了C#獲取客戶端IP地址、網(wǎng)絡(luò)連接、硬件信息等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Winform使用DataGridView實(shí)現(xiàn)下拉篩選
這篇文章主要為大家詳細(xì)介紹了Winform如何使用原生DataGridView實(shí)現(xiàn)下拉篩選功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-09-09
C#中DataTable和List互轉(zhuǎn)的示例代碼
很多場景下,我們需要將List轉(zhuǎn)換成為DataTable,本文主要介紹了C#中DataTable和List互轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方法
這篇文章主要介紹了C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方法,涉及C#窗體及鼠標(biāo)事件響應(yīng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#動態(tài)加載組件后如何在開發(fā)環(huán)境中調(diào)試詳解
這篇文章主要給大家介紹了關(guān)于C#動態(tài)加載組件后如何在開發(fā)環(huán)境中調(diào)試的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對網(wǎng)站進(jìn)行模擬登陸和瀏覽實(shí)例
這篇文章主要介紹了基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對網(wǎng)站進(jìn)行模擬登陸和瀏覽的方法,以實(shí)例形式詳細(xì)分析了WebClient模擬POST與GET登陸與瀏覽的過程,對于C#項(xiàng)目開發(fā)來說具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11

