欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2023年01月30日 10:47:33   作者:王彥杰698  
本文主要介紹了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()); //在這里,視覺樣式是默認的樣式,以系統(tǒng)的默認樣式為基準
    } 
}
 
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>
    /// 應(yīng)用程序的主入口點
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.Run(new MainForm()); //在這里,視覺樣式是默認的樣式,以系統(tǒng)的默認樣式為基準
    } 
}
 
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()導致執(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)閉父窗體,請確認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#?獲取本機IP地址(IPv4和IPv6)

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

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

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

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

    C#中radioButton控件使用詳細方法示例

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

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

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

    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#實現(xiàn)在前端網(wǎng)頁彈出警告對話框(alert)的方法,涉及C#通過自定義函數(shù)調(diào)用window.alert方法彈出對話框的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • 如何在C#中使用注冊表

    如何在C#中使用注冊表

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

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

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

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

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

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

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

最新評論