Say textBox1 and canelButton and the control names, then this is how you could do this:
[C#]
using System.ComponentModel;
// Handler to the Validating event of the TextBox.
private void TextBox_Validating( object sender, CancelEventArgs e )
{
// Do this only when the cancel button is not clicked.
if ( !cancelButton.Focused )
if ( invalidState )
e.Cancel = true;
}
Contributed from George Shepherd's Windows Forms FAQ