locked
Empty Text Box Error C# RRS feed

  • Question

  • i wanna make an output in a textBlock gives "Error" when a textBox is empty and this code there's something wrong with it :


            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                double  area;
                double r;
                r = Convert.ToInt32(textBox1.Text);
                area = 3.14 *r*r;
                if (textBox1.Text == "" || r<=0)
                {
                    Output2.Text = "Please Enter a value";
                }
                else
                {
                    output.Text = "The Area of your Circle is" + area;
                }
      

    Wednesday, December 11, 2013 9:05 PM

Answers

  • i tried and didn't work 
    but thanks guys i did it 

    Emam

    Thursday, December 12, 2013 3:31 PM

All replies

  • A few problems.

    You are converting output of textbox to an int but assigning that to a double.

    Good coding would use a try/ catch around the convert.toint to catch any errors.

    Instead of using ="" I would use if string length operator ==0


    n.Wright

    Wednesday, December 11, 2013 9:40 PM
  • So can you type me the code plz that i need cause i am not sure that i get you

    Emam

    Wednesday, December 11, 2013 10:07 PM
  •  

    Something like:

    private void Button_Click_1(object sender, RoutedEventArgs e)
           
    {
               
    double  area;
               
    double r;

                try

                {

                 r = Convert.ToDouble(textBox1.Text);

                }

                catch

               {

                 Output2.Text = "Invalid number";
               }

               area = 3.14 *r*r;
               
    if (textBox1.Text>Length == 0 || r<=0)
               
    {
                   
    Output2.Text = "Please Enter a value";
               
    }
               
    else
               
    { output.Text = "The Area of your Circle is" + area;
               
    }
     


    n.Wright

    Wednesday, December 11, 2013 10:12 PM
  • Length is unassigned ?!

    Emam

    Thursday, December 12, 2013 8:10 AM
  • Try using 

    Double.Parse(textBox1.Text);

    This should work

    Thursday, December 12, 2013 1:44 PM
  • i tried and didn't work 
    but thanks guys i did it 

    Emam

    Thursday, December 12, 2013 3:31 PM