locked
C# - Converting a string to a double with textboxes?

    Question

  • in my program I am using a database containing a product name and product cost that is associated with the product. On my form I have several comboboxes for several categories of products , with a textbox beside each combobox. When I select a product from the combobox , the products cost appears in the textbox beside it.

    What I want to do is add up all the cost of each textbox and display it in a textbox for the total price.
    I am having several problems with this. I believe I need to set each textbox to 0 else I get an error , but I cannot manage to convert each textbox to a double and I am at a loss as to how to do it.

    I am trying to set the values to 0 , and I have an array also

     

    private void btnCalculate_Click(object sender, EventArgs e)
        {
          double chip;
          double drink;
          double burger;
          double fish;
          double other;
          double tub;
          double chicken;
    
    
    
          chip = Convert.ToDouble(txtChipPrice.Text);
          drink = Convert.ToDouble(txtDrinkPrice.Text);
          burger = Convert.ToDouble(txtBurgerPrice.Text);
          fish = Convert.ToDouble(txtFishPrice.Text);
          other = Convert.ToDouble(txtPriceOther.Text);
          tub = Convert.ToDouble(txtTubPrice.Text);
          chicken = Convert.ToDouble(txtChickenPrice.Text);
    
          chip = 0.0;
          drink = 0.0;
          burger = 0.0;
          fish = 0.0;
          other = 0.0;
          tub = 0.0;
          chicken = 0.0;
    
    
          TextBox[] TextBoxList = new TextBox[] {txtChipPrice, txtDrinkPrice, txtBurgerPrice, txtFishPrice, txtPriceOther, txtTubPrice, txtChickenPrice };
          double total = 0;
          foreach (TextBox txt in TextBoxList)
          {
            
            if (Double.TryParse(txt.Text, out total) == false)
            {
              MessageBox.Show(txt.Name + ".Text is not numeric: " + txt.Text);
            }
    
          }
    
          txtTotalPrice.Text = total.ToString();
          
        }

    I have been literally hours at this and I think i am going insane , any suggestions?

     

    Thursday, April 29, 2010 1:06 AM

Answers

  • Hi,

    use double.tryparse instead of convert.todouble.

     

    Check link below:

    http://abansal.wordpress.com/2008/09/17/convert-parse-and-tryparse-methods-comparing-string-to-number-conversion-methods/

     

    Thanks

    Amit

    • Marked as answer by Harry Zhu Wednesday, May 05, 2010 5:18 AM
    Thursday, April 29, 2010 6:07 AM
  • Hi,

    Convert.ToDouble will fail if the TextBox does not contain a number.

    Using Double.TryParse as you do later in the code should work. An empty textbox might be considered equal to 0.  You do need to use a variable in TryParse other than total, though, or you will just overwrite the previous value.

    double total = 0;
    foreach (TextBox txt in TextBoxList)
    {
      double itemPrice;
      if (Double.TryParse(txt.Text, out itemPrice))
        total += itemPrice;
    }
    

    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by Harry Zhu Wednesday, May 05, 2010 5:17 AM
    Thursday, April 29, 2010 6:08 AM

All replies

  • There is another thread running on the same topi. please delete this one.

    Please do not forgot to mark it as an answer if it is. Thanks Gags
    Thursday, April 29, 2010 5:55 AM
  • Hi,

    use double.tryparse instead of convert.todouble.

     

    Check link below:

    http://abansal.wordpress.com/2008/09/17/convert-parse-and-tryparse-methods-comparing-string-to-number-conversion-methods/

     

    Thanks

    Amit

    • Marked as answer by Harry Zhu Wednesday, May 05, 2010 5:18 AM
    Thursday, April 29, 2010 6:07 AM
  • Hi,

    Convert.ToDouble will fail if the TextBox does not contain a number.

    Using Double.TryParse as you do later in the code should work. An empty textbox might be considered equal to 0.  You do need to use a variable in TryParse other than total, though, or you will just overwrite the previous value.

    double total = 0;
    foreach (TextBox txt in TextBoxList)
    {
      double itemPrice;
      if (Double.TryParse(txt.Text, out itemPrice))
        total += itemPrice;
    }
    

    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by Harry Zhu Wednesday, May 05, 2010 5:17 AM
    Thursday, April 29, 2010 6:08 AM
  • There is another thread running on the same topi. please delete this one.

    Please do not forgot to mark it as an answer if it is. Thanks Gags

    There is, but the other thread has incomplete code
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Thursday, April 29, 2010 6:09 AM