日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:王彥杰698 ? 更新時間: 2023-03-26 編程語言

我上次編寫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

欄目分類
最近更新