代码之家  ›  专栏  ›  技术社区  ›  user35978

C#关闭表单的好方法

  •  1
  • user35978  · 技术社区  · 17 年前

    TestForm test = new TestForm(); 
    

    关闭它的好方法是什么?更准确地说,只调用close()行吗? 如果一个对话框应该只打开一个对话框实例,是否存在 和null,在关闭时对其执行“ing”操作。。

    谢谢

    2 回复  |  直到 17 年前
        1
  •  4
  •   Mitch Wheat    17 年前

    这是一个很好的实践 using IDisposable . 这仅适用于模式对话框:

    using (TestForm test = new TestForm())
    {
        ....
    }
    

    它在语法上等同于 try{}finally{} 块,因此即使在花括号内抛出异常, Dispose() 将在您的对象上调用。

        2
  •  1
  •   Quintin Robinson    17 年前

    public class MyForm : Form
    {
        protected MyForm()
        {
        }
    
        protected int MyValue { get; set; }
    
        public static int GetResult()
        {
            using(MyForm myForm = new MyForm())
            {
                if(myForm.ShowDialog == DialogResult.OK)
                    return myForm.MyValue;   
            }
            return -1;
        }
    }
    

    这将迫使以..的形式拨打电话。。

    int someValue = MyForm.GetResult();
    
    推荐文章