0

I have a winform app.

I am using a numeric textbox control. My problem is that I can erase the value and leave the control. When I do that - the value that was before I delete it is the value of the control, but the display is an empty value. It is important to mention - The default value of the controls is 0. I would like that if the user deletes the value of the control , I would like to set the value of the control to 0.

My question is :

How can I force a numeric textbox control not to display an empty value ??

Thanks .

subirshan
  • 323
  • 2
  • 7
  • 20

1 Answers1

0

well, you can write a simple validation

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(textBox1.Text))
    {
        textBox1.Text = "0";
    }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • 1
    To expand on this answer, I'd be inclined to swap string.IsNullOrEmpty for string.IsNullOrWhiteSpace if you're using .NET4+ to cover the case where a space or other whitespace is entered by the user. – pwdst Mar 30 '13 at 19:17
  • @pwdst - thnx! i didnt knew MS added that in .Net 4, i am still stuck with 3.5 for my development works! – Parimal Raj Mar 30 '13 at 19:18
  • Thanks for all your answers but it is not as simple as you think. Numeric texbox is different from regular textbox. it doesn't have the 'Text' property. It has 'Value' property . And this property is never empty , dispite the fact that the control displays an empty string. – subirshan Mar 30 '13 at 19:30
  • @subirshan - pls show us what have to tried yet, so we may have a better idea. – Parimal Raj Mar 30 '13 at 19:31
  • For example - I set the value to 10. I leave the control , and then get back to it. now I delete the value and leave the control. at this point the value is stil 10 (I run it with debug mode) . So the ValueChange event don't fire . But the display is an empty string. – subirshan Mar 30 '13 at 19:40