On Visual Studio C# Express 2010, I find cancelling .Validating by setting e.Cancel as per the docs causes the app to hang upon exit. e.g. run the below and click title bar ‘X’.
Anyone know a solution? Thanks.
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;
}
}
}
The form is prevented from being closed as
textBox1is not valid – if you still want to allow the user to close the form then you can handle theFormClosingevent like this:You need to wire up this event in the normal way, for example:
If you debug the above event handler you will see that
e.Cancelistrueif your validation event hanlder sete.Cancelto be true.