4

I need to use basic functionality of the MaskedTextBox. I can get use of the 5 digit mask but there are few things that I want to change. Right now the box is looking like this:

MaskedTextBox

and there are two thing I don't like. First - the Prompt char which is undersoce _. I deleted the field value in order to leave it empty (as I would like it to appear) but this gives an error - The property value is invalid. So is there a way to get rid of these underscores? And second - I use this value for one of my entity properties which is of integer type so I make a convertion :

if (txtNumOfAreas.Text != "")
            {
                string temp = txtNumOfAreas.Text;
                bool result = Int32.TryParse(temp, out convertValue);
                if (result)
                {
                    entity.AreasCnt = convertValue;
                }
                else
                {
                    MessageBox.Show(Resources.ERROR_SAVE, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                entity.AreasCnt = null;
            }

which works fine unless someone decides to make experiments and insert something like _1__5_ then the conversion fails, but at first place I would like to make possible to write the digits only one after another. So is this possible too?

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
Leron
  • 9,546
  • 35
  • 156
  • 257
  • 1
    A little googling does it: [SO](http://stackoverflow.com/questions/1459797/hiding-the-promptchar-for-nets-maskedtextbox) and [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask(VS.90).aspx). – bash.d Mar 13 '13 at 15:40

2 Answers2

3

It looks like your MaskedEdit is more trouble than it's worth to deal with your particular range of issues. A better control to use might be the NumericUpDown.

The upside to NumericUpDown:

  • There are no underscore prompts to try and get rid of
  • It will only accept numeric input
  • So there is no need to try and convert the value. You will always have an integer
  • Setting Minimum and Maximum value properties gives you automatic data entry validation
  • Provides multiple modes of data entry:
    • Typing
    • Clicking up/down buttons with a mouse
    • Up/down with keyboard
  • If you like, you could hide the up/down buttons altogether: C# WinForms numericUpDown control (removing the spin box)
Community
  • 1
  • 1
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • @MichaelPerrenoud: Thanks, +1 for your nice answer too! – Paul Sasik Mar 13 '13 at 15:51
  • Hey, thanks a lot! It's a lot better when people aren't out here to cut each others throats but are just trying to help. – Mike Perrenoud Mar 13 '13 at 15:53
  • Yes, you are right, the `NumericUpDown` meets my needs much better. I'm gonna use it instead of the `MaskedTextBox`. Thanks. – Leron Mar 13 '13 at 15:54
  • @MichaelPerrenoud: Yeah, the rush for rep often leads to some ugly behavior, especially from lower-rep peeps trying to earn more. I really liked your response because I often get voted down offering an alternative suggestion. "But you didn't answer the question! -1" is how it usually goes. Not too frequent but terribly short-sighted. – Paul Sasik Mar 13 '13 at 15:58
3

So to get the _ to be a space you just need to set the mask character to a single space. And to resolve the conversion error, just set the AllowPromptAsInput property to false so that the user can't actually end up with something like your example.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232