<p>I am a MFC programmer who is new to C# and am looking for a simple control that will allow number entry and range validation.</p>

[{'answer_id': 77008, 'author': 'Adam Hawkes', 'author_id': 6703, 'author_profile': 'https://Stackoverflow.com/users/6703', 'pm_score': 4, 'selected': True, 'text': '<p>Look at the "NumericUpDown" control. It has range validation, the input will always be numeric, and it has those nifty increment/decrement buttons.</p>\n'}, {'answer_id': 77011, 'author': 'paulwhit', 'author_id': 7301, 'author_profile': 'https://Stackoverflow.com/users/7301', 'pm_score': 0, 'selected': False, 'text': '<p>You can use a regular textbox and a Validator control to control input.</p>\n'}, {'answer_id': 77116, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': "<p>Try using an error provider control to validate the textbox.  You can use int.TryParse() or double.TryParse() to check if it's numeric and then validate the range.</p>\n"}, {'answer_id': 77219, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 0, 'selected': False, 'text': '<p>You can use a combination of the RequiredFieldValidator and CompareValidator (Set to DataTypeCheck for the operator and Type set to Integer)</p>\n\n<p>That will get it with a normal textbox if you would like, otherwise the recommendation above is good.</p>\n'}, {'answer_id': 77853, 'author': 'Ricardo Amores', 'author_id': 10136, 'author_profile': 'https://Stackoverflow.com/users/10136', 'pm_score': 1, 'selected': False, 'text': '<p>I had to implement a Control which only accepted numbers, integers or reals.\nI build the control as a specialization of (read: derived from) TextBox control, and using input control and a regular expresión for the validation. \nAdding range validation is terribly easy.</p>\n\n<p>This is the code for building the regex. _numericSeparation is a string with characters accepted as decimal comma values\n(for example, a \'.\' or a \',\': $10.50 10,50€</p>\n\n<pre><code>private string ComputeRegexPattern()\n{\n   StringBuilder builder = new StringBuilder();\n   if (this._forcePositives)\n   {\n       builder.Append("([+]|[-])?");\n   }\n   builder.Append(@"[\\d]*((");\n   if (!this._useIntegers)\n   {\n       for (int i = 0; i &lt; this._numericSeparator.Length; i++)\n       {\n           builder.Append("[").Append(this._numericSeparator[i]).Append("]");\n           if ((this._numericSeparator.Length &gt; 0) &amp;&amp; (i != (this._numericSeparator.Length - 1)))\n           {\n               builder.Append("|");\n           }\n       }\n   }\n   builder.Append(@")[\\d]*)?");\n   return builder.ToString();\n}\n</code></pre>\n\n<p>The regular expression matches any number (i.e. any string with numeric characters) with only one character as a numeric separation, and a \'+\' or a \'-\' optional character at the beginning of the string.\nOnce you create the regex (when instanciating the Control), you check if the value is correct overriding the OnValidating method. \nCheckValidNumber() just applies the Regex to the introduced text. If the regex match fails, activates an error provider with an specified error (set with ValidationError public property) and raises a ValidationError event.\nHere you could do the verification to know if the number is in the requiered range.</p>\n\n<pre><code>private bool CheckValidNumber()\n{\n   if (Regex.Match(this.Text, this.RegexPattern).Value != this.Text)\n   {\n       this._errorProvider.SetError(this, this.ValidationError);\n       return false;\n   }\n   this._errorProvider.Clear();\n   return true;\n}\n\nprotected override void OnValidating(CancelEventArgs e)\n{\n   bool flag = this.CheckValidNumber();\n   if (!flag)\n   {\n      e.Cancel = true;\n      this.Text = "0";\n   }\n   base.OnValidating(e);\n   if (!flag)\n   {\n      this.ValidationFail(this, EventArgs.Empty);\n   }\n}\n</code></pre>\n\n<p>As I said, i also prevent the user from input data in the text box other than numeric characteres overriding the OnKeyPress methdod:</p>\n\n<pre><code>protected override void OnKeyPress(KeyPressEventArgs e)\n{\n    if ((!char.IsDigit(e.KeyChar) &amp;&amp; !char.IsControl(e.KeyChar)) &amp;&amp; (!this._numberSymbols.Contains(e.KeyChar.ToString()) &amp;&amp; !this._numericSeparator.Contains(e.KeyChar.ToString())))\n    {\n        e.Handled = true;\n    }\n    if (this._numberSymbols.Contains(e.KeyChar.ToString()) &amp;&amp; !this._forcePositives)\n    {\n        e.Handled = true;\n    }\n    if (this._numericSeparator.Contains(e.KeyChar.ToString()) &amp;&amp; this._useIntegers)\n    {\n        e.Handled = true;\n    }\n    base.OnKeyPress(e);\n}\n</code></pre>\n\n<p>The elegant touch: I check if the number valid every time the user releases a key, so the user can get feedback as he/she types. (But remember that you must be carefull with the ValidationFail event ;))</p>\n\n<pre><code>protected override void OnKeyUp(KeyEventArgs e)\n{\n    this.CheckValidNumber();\n    base.OnKeyUp(e);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76963', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']