0

I asked how to format a Textbox to accept only numbers but was adviced to use a Masked Textbox and set the Mask Property but doing this i have encountered some Problems

1) The masked textbox requires a maximum number of data a user can type to be set, but i want the user to be able to enter unlimited data

2) The masked textbox shows an Underscore

Underscore

how do i remove this??

Any help will be appreciated sorry if this question is not well structured

  • 1
    This is almost an [XY Problem](http://meta.stackexchange.com/a/66378). Instead you should have probably just searched for [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers). – Erik Philips Jul 08 '13 at 23:24
  • If you want unlimited data entry just set to `int.Max`, as they wont be able to enter more than that anyway on a 32bit platform – sa_ddam213 Jul 08 '13 at 23:27

2 Answers2

1

By default MaskedTextbox's PromptChar is set to "_" (underscore). Simply change its PromptChar property to " " (space)

coolmine
  • 4,427
  • 2
  • 33
  • 45
1

You could use a regular textbox and just handle the KeyPressed Event. This will also prevent Copy and paste, This was taken from another post here, How do I make a textbox that only accepts numbers? .

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) 
        && !char.IsDigit(e.KeyChar) 
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }
}
Community
  • 1
  • 1
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
  • Scratch that does not handle copy and paste but gets you further along than before! – Bearcat9425 Jul 08 '13 at 23:28
  • 1
    If you want to prevent copy and paste to this field you can make the shortcuts enabled property false on that textbox. – Bearcat9425 Jul 08 '13 at 23:30
  • 1
    Or simply handle the keydown event and check if the contents in the clipboard is a digit, then just append the text depending where the caret is. That way you will still be able to keep the copy/paste functionality. – coolmine Jul 08 '13 at 23:33