albert albert

albert albert

  • NA
  • 524
  • 0

How to calculate 2 textfields with eatch other?

Sep 22 2011 9:04 AM
Hi everyone,

I have this:

[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OrderFoodApp
{
    public partial class Form1 : Form
    {

        private int items;
        private double price, total;
        private double ItemsPerProduct = 0.0;
        private double eggs = 1.91;
        private double milk = 1.47;
        private double bread = 2.12;
        OrderFood orderFood;
        public Form1()
        {
            InitializeComponent();
            orderFood = new OrderFood(Convert.ToInt32(txtInput.Text),(Convert.ToDouble(comboBox1.SelectedIndex.ToString())) );
          
            comboBox1.Items.Add("Eggs");
            comboBox1.Items.Add("Milk");
            comboBox1.Items.Add("bread");
          

            comboBox1.SelectedIndexChanged +=new EventHandler(Selected_index);
            btnAddToBasket.Click += new EventHandler(btnAddToBasket_Click);
           
        }

       protected void btnAddToBasket_Click(object sender, EventArgs e)
        {
            items = Convert.ToInt32(txtInput.Text);
            price = Convert.ToDouble(lbCostPerItem.Text);
           // total = Convert.ToDouble(txtOutput.Text);
            //price = Convert.ToDouble(comboBox1.SelectedItem.ToString() );
            //total = items * price;
            //txtOutput.Text = "$" + total;
            txtOutput.Text += "\n" + "$" + "\n" +  orderFood.CostOfFood + "\n";
            Invalidate();

            //txtOutput.Text = Convert.ToInt32(txtInput.Text) * Convert.ToInt32(comboBox1.SelectedItem);
               
               // txtInput.Text * comboBox1.SelectedItem.ToString();
               
                //txtInput.ToString() * comboBox1.SelectedItem.ToString();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        protected void Selected_index(Object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "Eggs")
            {
                lbCostPerItem.Text = eggs.ToString(); //"$" + 1.91;
            }
             if (comboBox1.SelectedItem.ToString() == "Milk")
            {
                lbCostPerItem.Text = milk.ToString(); //"$" + 1.147;
            }
            if (comboBox1.SelectedItem.ToString() == "bread")
            {
                lbCostPerItem.Text = bread.ToString(); //"$" + 2.12;
            }
            Invalidate();

       
        }

        private void label3_Click(object sender, EventArgs e)
        {
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
        }
    }
}

[/code]

AND the class Orderfood:

[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrderFoodApp
{
   public class OrderFood
    {
        //private int items = 0;
        //private double price, total;
        private double ItemsPerProduct = 0.0;
        private double eggs = 1.91;
        private double milk = 1.47;
        private double bread = 2.12;

        public OrderFood(int Items, double  Price)
        {
            this.items = Items;
            this.price = price;
            //this.costOfFood = CostOfFood;
           

        }

        private double price;
        public double Price { get;private set; }

        private int items;
        public int Items { get; private set; }

        private double costOfFood;
        public double CostOfFood
        {
            get
            {
                return costOfFood;
            }
            set
            {
                costOfFood = value;
                costOfFood = items * price;
            }
        }
    }
}

But if I try tho calculate for example the eggs with 10 then I dont get the correct value.

Thx for helping!!

[/code]

Answers (2)