Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am experiencing an odd problem with trying to compare a value from a text box

I have used the following code but cannot find a way to get the expected outcome as if the text box value is a decimal 3.5 etc it will always enter the if statement.

C#
// Ensure text box is kept bellow the maximum value of an overrun
decimal maxOverrun = 15;
if (Decimal.Parse(tbOverrun.Text) > maxOverrun)
{
    tbOverrun.Text = "15";
}


thanks George
Posted
Comments
Amund Gjersøe 28-Jul-11 6:59am    
Thats strange. Do you do the test in the Validating-event? And just curious: Do you use the datatype decimal because you want to accept decimals in the textbox? Decimal is a 128-bit floating-point datatype, and the double datatype is more common.
grwithey 28-Jul-11 8:05am    
I was testing this upon leaving a text box.

There was no reson for the decimal in this case other than experimentation of different types to see if they worked.

There was a different problem here if i enter "1.5" it will convert this value to "15" as it is in German. I cannot figure out how to overcome this issue. it will work fine with a "," instead of a "."

Thanks George

1 solution

You should cast the text value first before you compare, something like this:

C#
decimal maxOverrun = 15;
            decimal userValue;
            decimal.TryParse(tbOverrun.Text, out userValue);
            if (userValue > maxOverrun)
            {
                //Do stuff here
            }


Hope this helps
 
Share this answer
 
Comments
grwithey 28-Jul-11 7:07am    
I think this should normally work however if i enter 1.321 in the text box the user value is set to "1321" which must be why it was entering the if.

I do have my pc set to German currently which may have something to do with it
grwithey 28-Jul-11 7:09am    
Yes it was because it was in German, if i replace the "." with a "," it works fine. Thanks Wayne for the answer.
Wayne Gaylard 28-Jul-11 7:19am    
Glad you got it sorted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900