10 Replies - 634 Views - Last Post: 28 April 2010 - 05:37 PM Rate Topic: -----

#1 SGstudent2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 10

Converting a string to a double involving textboxes?

Posted 28 April 2010 - 03:51 PM


TextBox[] TextBoxList = new TextBox[] { txtChipPrice, txtDrinkPrice, txtBurgerPrice, txtFishPrice, txtPriceOther, txtTubPrice, txtChickenPrice };
            double total = 0;
            foreach (TextBox txt in TextBoxList)
            {
                total += Convert.ToDouble(txt.SelectedText);
            }

            txtTotalPrice.Text = total.ToString();




What I'm doing is I'm adding together a collection of product prices in different Textboxes into 1 textbox to get the total price.

I keep getting an error message saying I can't change a string to a double.

"Input string was not in a correct format."

The line of code with the problem is :

total += Convert.ToDouble(txt.SelectedText);

Can anybody help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting a string to a double involving textboxes?

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 04:04 PM

See
Double.Parse Method (String)

or

Double.TryParse Method (String, Double%)

Edit:

Are you sure you want

total += Convert.ToDouble(txt.SelectedText);



instead of

total += Convert.ToDouble(txt.Text);


This post has been edited by n8wxs: 28 April 2010 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 SGstudent2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 10

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 04:16 PM

Hello n8wxs ,

I've tried

Convert.ToDouble(txt.SelectedText);

Convert.ToDouble(txt.Text);

Double.Parse(txt.SelectedText);

And no luck

It keeps giving me "Input string was not correct". :/
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is online

  • GabehabeSwamp
  • member icon




Reputation: 1346
  • View blog
  • Posts: 10,918
  • Joined: 06-February 08

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 04:18 PM

You're using SelectedText, did you mean to use Text? SelectedText varies depending on the text that's been highlighted within the textbox! ;)

edit: cock it, I'm off my game. Used to be able to get first all the time. ;)

edit2: noticed you're still not having any luck, don't forget you're looping through several textboxes. Are some of them empty, or do they all at least have a default value in them? Might be an idea to set the default value of them all to 0.0 and have them validate to only accept a numeric format! :)

This post has been edited by gabehabe: 28 April 2010 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 SGstudent2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 10

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 04:25 PM

What the program is about is I have created a datatable with a product name and a set product cost associated with each product.
I choose a product from a ComboBox when that is selected its cost appears in a textbox near it , in the data definition the cost is set as a float.

there are several ComboBoxes with separate categories of food , I add all the costs together into 1 textbox.

I've only got that 1 problem left :/

Yeah I'm using several textboxes some of them are empty it depends on if I choose products from several categories. None of them are set to default I'm not entirely sure how to do that
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 04:28 PM

View PostSGstudent2010, on 28 April 2010 - 03:16 PM, said:

Hello n8wxs ,

I've tried

Convert.ToDouble(txt.SelectedText);

Convert.ToDouble(txt.Text);

Double.Parse(txt.SelectedText);

And no luck

It keeps giving me "Input string was not correct". :/



Well then, your textbox(s) contain none numeric text. The TryParse() method returns a boolean rather than throwing an exception so you could recover from the error. Also you could use MessaageBox.Show() to see which text is faulting.
Was This Post Helpful? 0
  • +
  • -

#7 SGstudent2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 10

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 04:40 PM

To be honest I do not know how to operate the double.TryParse method , it requires 2 arguments but I do not know what I am to enter for the 2nd argumenet
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 05:02 PM

The second argument is the variable you would assign the returned value to if the method returned a double.
Like this:

TextBox[] TextBoxList = new TextBox[] { txtChipPrice, txtDrinkPrice, txtBurgerPrice, txtFishPrice, txtPriceOther, txtTubPrice, txtChickenPrice };
double total = 0;
double temp;   // target of TryParse() conversion

foreach (TextBox txt in TextBoxList)
{
   if (Double.TryParse(txt.Text, out temp) == true)
   {
      total += temp;
   }
   else
   {
      MessageBox.Show(txt.Name + ".Text is not numeric: " + txt.Text);
   }
}

txtTotalPrice.Text = total.ToString();


This post has been edited by n8wxs: 28 April 2010 - 05:08 PM

Was This Post Helpful? 1
  • +
  • -

#9 SGstudent2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 10

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 05:13 PM

n8wxs,
How do you set each of the textboxes to default?
Also how do you add up all figures in the textboxes?
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 05:25 PM

TextBox doesn't have a default text property so your code will have to supply whatever default values you need.

The loop above does total the contents, provided the text is numeric.

This post has been edited by n8wxs: 28 April 2010 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#11 SGstudent2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 10

Re: Converting a string to a double involving textboxes?

Posted 28 April 2010 - 05:37 PM

ok what I've done for this is as follows

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;

I'm having the problem with this input string again , I'm not sure if I'm doing this right
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1