C# 如何避免.Validating()取消导致应用程序退出挂起?

C# 如何避免.Validating()取消导致应用程序退出挂起?,c#,.net,visual-studio,C#,.net,Visual Studio,在Visual Studio C#Express 2010上,我发现正在取消。根据文档设置e.Cancel进行验证会导致应用程序在退出时挂起。e、 g.运行以下命令并单击标题栏“X” 有人知道解决办法吗?谢谢 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sy

在Visual Studio C#Express 2010上,我发现正在取消。根据文档设置e.Cancel进行验证会导致应用程序在退出时挂起。e、 g.运行以下命令并单击标题栏“X”

有人知道解决办法吗?谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}

表单被阻止关闭,因为
textBox1
无效-如果您仍然希望允许用户关闭表单,则可以如下处理
FormClosing
事件:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = false;
}
您需要以正常方式连接此事件,例如:

this.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing);
如果您调试上述事件处理程序,您将看到
e.Cancel
true
如果您的验证事件handler将
e.Cancel
设置为true