Answered by:
Textbox min max characters is not working

Question
-
Hi Guys,
I want to assigan a decimal values betwwel -50 to 50 in the textbox range and if the type any illagal ch or greather than 50..I wnat to show a error message.
My code is not working for both min and max value?? Any help??
decimal number; string value = t_LongitudinalSlopeRadTextBox.Text; if (!decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out number) || number < -50 || number >= 50) { errorProvider1.SetError(t_LongitudinalSlopeRadTextBox, "Needs to contain decimal numbers"); } else { errorProvider1.Clear(); errorProvider1.SetError(t_LongitudinalSlopeRadTextBox, ""); }
Answers
-
Is that because in you first post, you make you statement with a different operator !try.Parse...., and now he is doing without it, just change the order of messageboxes.
If you get your question answered, please come back and
Mark As Answer.
Web Developer -
51 and 52 are larger (or equal) to 50 so the statement returns true.
Try this:
if (decimal.TryParse(value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out number) && number > -50 && number <= 50 ) { // }
- Edited by Martijn Bos Tuesday, October 30, 2012 1:28 PM
- Marked as answer by KrKa2022 Tuesday, October 30, 2012 1:36 PM
All replies
-
I think it is related to commas ands dot. You should define what Culture will use for that. Or use InvariantCulture, in my case if my textbox comes 1.50, it will be 1.5 when parse it, but if it comes 1,50 will be 150 and won't work as well.
If you get your question answered, please come back and
Mark As Answer.
Web Developer -
-
i changed your condition, check this:
decimal number; string value = textBox3.Text; if (decimal.TryParse(value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out number) && (number < -50 || number >= 50) ) { MessageBox.Show("Yes, within the range"); } else { MessageBox.Show("No, invalid number or outside range"); }
In your code, you are checking whether it is a decimal number or not with any value less than -50, or greater than equal 50.
But you can revert back the condition, I changed first check whether it is decimal number and any of the above condition should be fulfilled.
regards
joon
- Edited by Joon84 Tuesday, October 30, 2012 12:10 PM
-
-
i changed your condition, check this:
decimal number; string value = textBox3.Text; if (decimal.TryParse(value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out number) && (number < -50 || number >= 50) ) { MessageBox.Show("Yes, within the range"); } else { MessageBox.Show("No, invalid number or outside range"); }
In your code, you are checking whether it is a decimal number or not with any value less than -50, or greater than equal 50.
But you can revert back the condition, I changed first check whether it is decimal number and any of the above condition should be fulfilled.
regards
joon
k, i replaced the message box with error provider but it's not working. What should i do??
How can i show red color if there is a wrong value in the textbox?? Thank you
- Edited by KrKa2022 Tuesday, October 30, 2012 1:02 PM edited
- Proposed as answer by Martijn Bos Tuesday, October 30, 2012 1:30 PM
-
With normal textbox, you can set the color of the text to red or so.
textBox3.ForeColor = Color.Red;
If you wish to have more formatting, you can always use RichTexrBox control.
http://msdn.microsoft.com/en-us/library/aa970779(v=vs.100).aspx
regards
joon
-
-
When i use this, even if i type 51 or 52 it says "yes, within range".... What is the problem??
decimal number; string value = t_LongitudeRadTextBox.Text; if (decimal.TryParse(value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out number) && (number < -50 || number >= 50)) { MessageBox.Show("Yes, within the range"); t_LongitudeRadTextBox.ForeColor = Color.Black; } else { MessageBox.Show("No, invalid number or outside range"); t_LongitudeRadTextBox.ForeColor = Color.Red; }
- Edited by KrKa2022 Tuesday, October 30, 2012 1:23 PM edited
-
Is that because in you first post, you make you statement with a different operator !try.Parse...., and now he is doing without it, just change the order of messageboxes.
If you get your question answered, please come back and
Mark As Answer.
Web Developer -
51 and 52 are larger (or equal) to 50 so the statement returns true.
Try this:
if (decimal.TryParse(value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out number) && number > -50 && number <= 50 ) { // }
- Edited by Martijn Bos Tuesday, October 30, 2012 1:28 PM
- Marked as answer by KrKa2022 Tuesday, October 30, 2012 1:36 PM