C# try catch代码块不起效果的解决方法
作者:王彦杰698
本文主要介绍了C# try catch代码块不起效果的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
我上次编写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,再关闭父窗体,执行完所有代码之后,清除这个控件类。
到此这篇关于C# try catch代码块不起效果的解决方法的文章就介绍到这了,更多相关C# try catch代码块不起效果内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!