C# try catch代碼塊不起效果的解決方法
我上次編寫winform程序時發(fā)生了ObjectDisposedException,后來使用了try catch代碼塊還是沒有解決,源代碼:
using System.Windows.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點
/// </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)于不存在,也就是單行注釋,但不能刪除,因為有region
加上try catch代碼塊以后:
?using System.Windows.Forms;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點
/// </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)閉這個控件類的父窗體,如要關(guān)閉父窗體,請確認(rèn)Close函數(shù)要在所有代碼之后,否則設(shè)置這個控件類的父容器為null,再關(guān)閉父窗體,執(zhí)行完所有代碼之后,清除這個控件類。
到此這篇關(guān)于C# try catch代碼塊不起效果的解決方法的文章就介紹到這了,更多相關(guān)C# try catch代碼塊不起效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Tesseract進(jìn)行Ocr識別的方法實現(xiàn)
本文主要介紹了C#使用Tesseract進(jìn)行Ocr識別的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#實現(xiàn)十六進(jìn)制與十進(jìn)制相互轉(zhuǎn)換以及及不同進(jìn)制表示
在C#中十進(jìn)制和十六進(jìn)制轉(zhuǎn)換非常簡單,下面這篇文章主要給大家介紹了關(guān)于C#實現(xiàn)十六進(jìn)制與十進(jìn)制相互轉(zhuǎn)換以及及不同進(jìn)制表示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
C#如何將查詢到的數(shù)據(jù)庫里面的數(shù)據(jù)輸出到textbox控件
這篇文章主要介紹了C#如何將查詢到的數(shù)據(jù)庫里面的數(shù)據(jù)輸出到textbox控件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
C#實現(xiàn)在前端網(wǎng)頁彈出警告對話框(alert)的方法
這篇文章主要介紹了C#實現(xiàn)在前端網(wǎng)頁彈出警告對話框(alert)的方法,涉及C#通過自定義函數(shù)調(diào)用window.alert方法彈出對話框的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04

