亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# try catch代碼塊不起效果的解決方法

 更新時(shí)間:2023年01月30日 10:47:33   作者:王彥杰698  
本文主要介紹了C# try catch代碼塊不起效果的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

我上次編寫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é):盡量不要手動(dòng)添加代碼關(guān)閉這個(gè)控件類的父窗體,如要關(guān)閉父窗體,請(qǐng)確認(rèn)Close函數(shù)要在所有代碼之后,否則設(shè)置這個(gè)控件類的父容器為null,再關(guān)閉父窗體,執(zhí)行完所有代碼之后,清除這個(gè)控件類。

到此這篇關(guān)于C# try catch代碼塊不起效果的解決方法的文章就介紹到這了,更多相關(guān)C# try catch代碼塊不起效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#?獲取本機(jī)IP地址(IPv4和IPv6)

    C#?獲取本機(jī)IP地址(IPv4和IPv6)

    本文主要介紹了C#?獲取本機(jī)IP地址(IPv4和IPv6),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • C#使用Tesseract進(jìn)行Ocr識(shí)別的方法實(shí)現(xiàn)

    C#使用Tesseract進(jìn)行Ocr識(shí)別的方法實(shí)現(xiàn)

    本文主要介紹了C#使用Tesseract進(jìn)行Ocr識(shí)別的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C#中radioButton控件使用詳細(xì)方法示例

    C#中radioButton控件使用詳細(xì)方法示例

    這篇文章主要給大家介紹了關(guān)于C#中radioButton控件使用詳細(xì)方法的相關(guān)資料,RadioButton是圓形單選按鈕,在同一個(gè)容器中,單選項(xiàng)互斥,不同容器中的RadioButton互相獨(dú)立,需要的朋友可以參考下
    2023-10-10
  • C#實(shí)現(xiàn)十六進(jìn)制與十進(jìn)制相互轉(zhuǎn)換以及及不同進(jìn)制表示

    C#實(shí)現(xiàn)十六進(jìn)制與十進(jìn)制相互轉(zhuǎn)換以及及不同進(jìn)制表示

    在C#中十進(jìn)制和十六進(jìn)制轉(zhuǎn)換非常簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于C#實(shí)現(xiàn)十六進(jìn)制與十進(jìn)制相互轉(zhuǎn)換以及及不同進(jìn)制表示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • C#如何將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件

    C#如何將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件

    這篇文章主要介紹了C#如何將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C#實(shí)現(xiàn)在前端網(wǎng)頁彈出警告對(duì)話框(alert)的方法

    C#實(shí)現(xiàn)在前端網(wǎng)頁彈出警告對(duì)話框(alert)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)在前端網(wǎng)頁彈出警告對(duì)話框(alert)的方法,涉及C#通過自定義函數(shù)調(diào)用window.alert方法彈出對(duì)話框的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 如何在C#中使用注冊(cè)表

    如何在C#中使用注冊(cè)表

    這篇文章主要介紹了如何在C# 使用注冊(cè)表,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-12-12
  • Unity實(shí)現(xiàn)10天簽到系統(tǒng)

    Unity實(shí)現(xiàn)10天簽到系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)10天簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C#Url操作類封裝、仿Node.Js中的Url模塊實(shí)例

    C#Url操作類封裝、仿Node.Js中的Url模塊實(shí)例

    這篇文章主要介紹了C#Url操作類封裝、仿Node.Js中的Url模塊,實(shí)例分析了C#Url操作類封裝的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2016-10-10
  • C# 線程相關(guān)知識(shí)總結(jié)

    C# 線程相關(guān)知識(shí)總結(jié)

    這篇文章主要介紹了C# 線程相關(guān)知識(shí),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評(píng)論