The Validating event was made to do that. Drop an ErrorProvider control on your form so you can subtly remind the user that she did something wrong. The event also allows you to format the text box text the way that make sense. Like this:
private void textBox1_Validating(object sender, CancelEventArgs e) {
// Empty strings okay? Up to you.
if (textBox1.Text.Length > 0) {
decimal value;
if (decimal.TryParse(textBox1.Text, out value)) {
textBox1.Text = value.ToString("N2");
errorProvider1.SetError(textBox1, "");
}
else {
e.Cancel = true;
textBox1.SelectAll();
errorProvider1.SetError(textBox1, "Please enter a number");
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…