Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top