亲宝软件园·资讯

展开

C# try catch代码块不起效果的解决方法

王彦杰698 人气:0

我上次编写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,再关闭父窗体,执行完所有代码之后,清除这个控件类。

加载全部内容

相关教程
猜你喜欢
用户评论