Russell Saari
Russell Saari

Reputation: 995

C# maskedTextBox Sum Help

Need help with getting sum from other masked textbox * .0628

   private void button11_Click(object sender, EventArgs e)
    {
        maskedTextBox2.Text = (
            maskedTextBox1.Text *.0628 + //Cost of Rail
            200
            ).ToString();
    }

Upvotes: 0

Views: 269

Answers (3)

vcsjones
vcsjones

Reputation: 141678

maskedTextBox1.Text is a string. You need to convert it to a numeric data type before you can do arithmetic on it. Decimal seems appropriate in this case. float or double may be appropriate as well. Make sure you understand the differences between all of them before picking one.

maskedTextBox2.Text = (
    Decimal.Parse(maskedTextBox1.Text) * 0.0628m + //Cost of Rail
    200
    ).ToString();

Note that you will probably want to do validation, etc. If the conversion fails, you will get an exception. You can test if the conversion works by using Decimal.TryParse.

Upvotes: 3

Senad Meškin
Senad Meškin

Reputation: 13756

private void button11_Click(object sender, EventArgs e)
    {
       decimal sum2 = 0;
       decimal.TryParse(maskedTextBox2.Text, out sum2);
        maskedTextBox2.Text = (
            sum2 * 0.0628m +  200
            ).ToString();
    }

first of all parse string from maskedTextBox2 as decimal then use it to create new value

second You can't write decimal numbers like 0.8888 you have to add m at the end so compiler can understand that entered value is decimal.

Upvotes: 2

TIHan
TIHan

Reputation: 632

Try this.

   private void button11_Click(object sender, EventArgs e)
    {
        maskedTextBox2.Text = (
            (float.Parse(maskedTextBox1.Text) *.0628 + //Cost of Rail
            200)
            ).ToString();
    }

Upvotes: 5

Related Questions