I have a datagrid that I have several columns defined, one of which I have an event handler created for validation. I have a function that I call for validation when the focus changes (click out of column or press TAB). The validation function fires fine if you click out, and keeps focus in the column if it fails, but if you press TAB, the function fires, but the focus still goes to the next field. How can I prevent the TAB to the next field if the function fails.
Ive run this through the debugger many times. It appears that the Validate event more than once for some reason. For instance, if I set e.Cancel = True, then show a Message Box, it will fire again after the Ok is clicked on the Messagebox. This is ONLY happening on Datagrid validation. Other textboxes defined on the for work fine. |
colStHrs.TextBox.Validating += new CancelEventHandler(StHrs_Validate);
.
private
void StHrs_Validate(Object sender, CancelEventArgs e)
{
TextBox txt = sender as TextBox;
if (txt.Text != "Check") {
e.Cancel = true;
MessageBox.Show("Should not be Check") // Sends error, and keeps focus in column if clicked out of. If TAB'd out of, focus changes
// How can I not allow the TAB to work if invalid, but still allow TABs if value is valid?
}
else
e.Cancel = false;