Hi, I have a textbox which I want to change the font colour when you input a number after a certain number (in my case Pi*2)..
Although the code I wrote gives me this error : Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'.
I don't understand the error..

So that's what I wrote:

if (Convert.ToDouble(textBox1.Text) > Math.PI*2)
                {
                    textBox1.BackColor = Color.Red;
                }

thanks for helping me, I truly appreciate it!

Please use CODE tags when posting. textBox1 would return a reference to the actual TextBox. textBox1.Text would return the string displayed in the TextBox.

woops forgot, how silly !

But then it gives me this error : Input string was not in a correct format.

What input was in the TextBox when this error occurred? You should wrap that if statement in a try-catch block in case the input is incorrect. Remember, an empty string or number with spaces may throw an error.

I'd use Double.TryParse as you can skip the try/catch exception.

Do you use a comma or a point as decimal separator? .NET prefers a point.

Do you use a comma or a point as decimal separator? .NET prefers a point.

Not necessarily, I believe .NET uses the system's regional settings to determine this, unless specific IFormatProvider is used. Here they refer to the decimal point as culture-specific.

@nmaillet
This compiles and runs:

class Program
    {
        static void Main(string[] args)
        {
            double d1 = double.Parse("123.456");//point
            double d2 = double.Parse("123,456");//comma
            Console.WriteLine("d1={0}  d2={1}",d1,d2);
            Console.ReadKey();
        }
    }

OUTPUT: d1=123456 d2=123,456
The same with TryParse.
My culture setting: Dutch(Belgium) comma
Because I tend to visit sites(eg. DaniWeb :) ) quite alot I get influenced by writing a point sometimes, when it has to be a comma overhere.
Without a warning in this situation I already had some fine time debugging, I can tell you!

Thanks a lot guys !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.