Hi Anonymous261977,
Based on your post, you want format the text when leaving the text box, don’t you? If so, I suggest you handle the Validating event instead, because you can check if input value is in right type in this handler. Try something like the following:
Code Block
TextBoxP
public partial class Form12 : Form
{
public Form12()
{
InitializeComponent();
this.textBox1.Validating += new CancelEventHandler(textBox1_Validating);
this.textBox1.Enter += new EventHandler(textBox1_Enter);
}
void textBox1_Enter(object sender, EventArgs e)
{
if (this.textBox1.Tag != null)
this.textBox1.Text = this.textBox1.Tag.ToString();
}
void textBox1_Validating(object sender, CancelEventArgs e)
{
try
{
decimal d = Decimal.Parse(this.textBox1.Text);
this.textBox1.Tag = this.textBox1.Text;
this.textBox1.Text = d.ToString("0.0000");
}
catch
{
MessageBox.Show("Can not accept this input!");
e.Cancel = true;
}
}
}
Hope this helps. Best regards. Rong-Chun Zhang |