curiousDev
curiousDev

Reputation: 437

How to replace deleted character with 0 in the textbox

In winforms, how to replace deleted character with zero. for example 12.45 is in the textbox. if we delete after decimal point, it should become 12.00. same way if deleted in the front, 000.45. and default value should be 000.00.

Upvotes: 3

Views: 391

Answers (2)

James
James

Reputation: 82136

Use a MaskedTextBox and set the Mask property to 0.00 and PromptChar property to 0 - this should give you behaviour close to what you are looking for.

However, there is a slight quirk for example if your value was 3.22 and you deleted 3 the other numbers would shift to the left, so the value would become 2.20 whereas you would probably expect the value to change to 0.22.

I think if you wanted that sort of control you would need to implement something custom by handling the key press events on the control.

Upvotes: 1

Sathish
Sathish

Reputation: 4487

Try This Also

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Enter) 
   { 
     textBox2.Text = String.Format("{0:000.00}", Convert.ToDouble(textBox2.Text));
   }
}

Upvotes: 0

Related Questions