Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Limiting textBox Input in C#
#1
I've been working on a calculator to compare experience per level and cumaltive experience in the three experience curves MapleStory uses. On a KeyPress event for every input box, I call this method.
Code:
private void WhenaKeyIsPressed(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
            {
                e.Handled = true;
            }
        }
The idea was to limit the input to numbers, so I wouldn't have to make fancy checks for letters, but a few problems arose.

  1. Backspace does not work. To delete something, I have to arrow key behind it and press Delete.
  2. Decimals do not work. I want it to take a decimal value for percents, but it blocks the "." key too.
I Googled around for Regular Expressions and tried a multitude of different things, but everything gave errors. I tried reading on how to format them. Errors. I tried copypasting premade ones. Errors.

I need some help with this. I want to limit all the inputs to integers, except for two which I want limited to decimals rounded to the nearest hundredth. How can I do this with Regular Expressions or with another method?
Reply


Messages In This Thread
Limiting textBox Input in C# - by Hazzy - 2010-08-15, 03:18 PM
Limiting textBox Input in C# - by JoeTang - 2010-08-15, 03:37 PM
Limiting textBox Input in C# - by Hazzy - 2010-08-15, 04:23 PM
Limiting textBox Input in C# - by Spaz - 2010-08-15, 09:09 PM
Limiting textBox Input in C# - by Kortestanov - 2010-08-16, 06:10 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)