hello guys im having a little problem here.
I have few text boxes and few labels. User enters values into textboxex. Values in lables are from XML file, so the program reads for egz <price>30</price> and this price is set as a label label1 => 30 And now im wondering how to multiply a value entered by user by the value of a label ( in this case 30, but can be any number)

thank you :)

        int answer1 = 0;            
        answer1 = (textboxnumber1/100)*label1 + textboxnumber2 + textboxnumber3 + textboxnumber4;
        total1.Text = answer1.ToString();



        "Error  1   Operator '*' cannot be applied to operands of type 'int' and 'System.Windows.Forms.Label'   c:\users\boss\documents\visual studio 2010\Projects\tabs\tabs\Form1.cs  103 23  tabs

"

Dani AI

Generated

The error you saw comes from trying to do arithmetic with a control object instead of a number: a Label is a System.Windows.Forms.Label, not an int or decimal. As discovered, you must use a numeric value (for example the label's Text parsed to a number) or, better, keep the numeric price as a decimal variable so calculations never touch the Label control itself.

A robust pattern: parse the XML price once when you load it, store the parsed numeric value (not just the formatted Text) and reuse that value during calculations. Storing the numeric value in the label's Tag is a simple option.

using System.Globalization;

// when reading XML
decimal price;
if (!decimal.TryParse(xmlPriceString, NumberStyles.Number | NumberStyles.AllowCurrencySymbol,
                      CultureInfo.InvariantCulture, out price))
{
    price = 0m;
}
labelPrice.Text = price.ToString("0.##");
labelPrice.Tag = price; // keep the numeric value handy

When computing totals, always work with numeric types (decimal is best for money) and avoid integer division by using decimal literals (e.g. 100m). Parse each textbox into a decimal (validate parse results) and then compute:

decimal qty = 0m, price = (labelPrice.Tag is decimal) ? (decimal)labelPrice.Tag : 0m;
decimal.TryParse(textBoxQty.Text, NumberStyles.Number, CultureInfo.CurrentCulture, out qty);

decimal total = (qty / 100m) * price
                + /* other parsed values */;
labelTotal.Text = total.ToString("F2");

Builds on and : parsing is required, but prefer parsing once and storing the numeric price. 's point about restricting input is valid—use NumericUpDown or validate on Leave/KeyPress to avoid bad input. Ignore the suggestion to convert numbers to strings for math (); keep values as numbers and format to string only for display. Also watch culture and currency symbols when parsing text.

Recommended Answers

All 6 Replies

You have to parse the 'string' data to the appropriate simple type before the calculation.

for instance,

double no1=double.Parse(textboxnumber1.Text);
double no2=double.Parse(Label1.Text);

You may also use XXXXX.TryParse method.

e.g

double no1;
double.TryParse(textboxnumber1.Text,out no1);

You can try like this also.....converting lable value to int

            int answer1 = 0;
            answer1 = (Convert.ToInt32(textboxnumber1.text) / 100) * Convert.ToInt32 (label1.text) + Convert.ToInt32(textboxnumber2.text) + Convert.ToInt32(textboxnumber3.text) + Convert.ToInt32(textboxnumber4.text);
            total1.Text = answer1.ToString();

Hope this will help.

Restrict your test box to only except ints so that the user can't enter charactors into the textbox.

Ok thx for help :)

convert the int value to string thn it will work effectively

convert the int value to string thn it will work effectively

This would mean that charactors can be entered into the textbox which would cause errors. Best thing to do would be restrict/ validate what the user can enter.

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.