如何以新表格更新订单总金额

时间:2019-06-15 11:00:10

标签: c#

大家好,我正在做的窗口表单应用程序是1.)将数据与文本文件连接起来,然后当用户从列表框中选择文本框中数量类型的项目时,它将计算成本。

2。)当用户单击确认btn后,用户订购的商品将存储在文本文件中并进行错误检查,检查用户订购的商品是否有空。

3。)我现在遇到的问题是他们需要使用摘要按钮,当单击它时它将在txtbox中显示用户订购的当前成本金额和项目金额,并以另一种形式显示用户订购的名称,数量项目 与3.)任何人有任何想法,请建议我解决这个问题,

这是我到目前为止完成的代码

public partial class Form1 : Form
{


    //All public variables
    double dblRetail;
    int intchoice;

    public static int intSaleCount = 0;
    public double dblTotalCost = 0;
    double dblCost;
    public static int Stock;
    public static int quantity;
    public static int price;
    public static int intQuantity;
    public static int retailPrice;
    public static int total;
    int selectedItem;



    public Form1()
    {
        InitializeComponent();
    }

    public struct Products
    {
        public string strCode;
        public string strName;
        public int intStock;

        public double dblPrice;
        public string strImage;

    };


    List<Products> lstProducts = new List<Products>();

    private void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //To select items in listbox and output to Picturebox and textbox
        selectedItem = listBox1.SelectedIndex;

        if (listBox1.SelectedIndex == -1)
        {
            txtRetailPrice.Clear();
        }
        else
        {             
            txtRetailPrice.Text = (lstProducts[selectedItem].dblPrice*1.5).ToString("c");
        }

        if(listBox1.SelectedIndex == -1)
        {
            pictureBox1.Image = null;
        }
        else
        {
            pictureBox1.Image = Image.FromFile(lstProducts[selectedItem].strImage);
        }
    }




    private void Button1_Click(object sender, EventArgs e)
    {


        //Calculate item order 

        if (!int.TryParse(txt_quantity.Text, out quantity)) 
        {
            MessageBox.Show("This is not a number – please enter it again");
            txt_quantity.Clear();
            txt_quantity.Focus();
            btn_confirm.Visible = false;
            btn_cancel.Visible = false;

        }
        if (listBox1.SelectedIndex == -1)
        {
            MessageBox.Show("You not order - please select item you want");
            txt_quantity.Clear();
            txt_quantity.Focus();
            btn_confirm.Visible = false;
            btn_cancel.Visible = false;

        }
        if (quantity == 0)
        {
            MessageBox.Show("You didn't order  – please enter it again");
            txt_quantity.Clear();
            txt_quantity.Focus();
            btn_confirm.Visible = false;
            btn_cancel.Visible = false;
        }

        else
        {

            txt_cost.Text = ((lstProducts[selectedItem].dblPrice * 1.5) * quantity).ToString("c");
            btn_confirm.Visible = true;
            btn_cancel.Visible = true;
        }




    }

    private void PictureBox1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {


        StreamReader inFile;
        inFile = File.OpenText("products.txt");
        string strRead;
        while ((strRead = inFile.ReadLine()) != null)
        {
            Products PrdObj;
            string[] strTempArr = new string[5];
            strTempArr = strRead.Split(',');
            PrdObj.strCode = strTempArr[0];
            PrdObj.strName = strTempArr[1];
            PrdObj.intStock = int.Parse(strTempArr[2]);
            PrdObj.dblPrice = double.Parse(strTempArr[3]);
            PrdObj.strImage = strTempArr[4];
            lstProducts.Add(PrdObj);        
        }
        foreach (Products PrdObj in lstProducts)
        {
            listBox1.Items.Add(PrdObj.strName);

            txt_quantity.SelectAll();
            txt_quantity.Focus();
            btn_confirm.Visible = false;
            btn_cancel.Visible = false;

            txt_quantity.Text = "0";


            this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

        }


        inFile.Close();


    }

    private void Btn_confirm_Click(object sender, EventArgs e)
    {

        //Store  items order
        Stock = lstProducts[selectedItem].intStock;

        //Check availebale in stock
        if (Stock < quantity)
        {
            MessageBox.Show("Item you ordered is not enough in stock " + "This item availeble with: " + lstProducts[selectedItem].intStock);
            txt_quantity.Clear();
            txt_cost.Clear();
            txt_quantity.Focus();
            btn_confirm.Visible = false;
            btn_cancel.Visible = false;
        }
        else
        {






            //Edit stock in txt file
            Products now = new Products();
            now.strCode = lstProducts[selectedItem].strCode;
            now.strName = lstProducts[selectedItem].strName;
            now.intStock = lstProducts[selectedItem].intStock - quantity;
            now.dblPrice = lstProducts[selectedItem].dblPrice;
            now.strImage = lstProducts[selectedItem].strImage;
            lstProducts.Add(now);

            //
            lstProducts.Remove(lstProducts[selectedItem]);
            listBox1.Items.RemoveAt(selectedItem);
            listBox1.Items.Add(now.strName);

            //Return to origin
            listBox1.ClearSelected();
            txt_quantity.Clear();
            txt_cost.Clear();
            btn_confirm.Visible = false;
            btn_cancel.Visible = false;

        }


    }

    private void Btn_cancel_Click(object sender, EventArgs e)
    {
        txtRetailPrice.Clear();
        txt_quantity.Clear();
        txt_cost.Clear();
        btn_confirm.Visible = false;
        btn_cancel.Visible = false;
    }

    private void Btn_salesummary_Click(object sender, EventArgs e)
    {




        // Declare the new display form variable and create the form object
        Form2 frm_show_data = new Form2();




        // Display the form
        frm_show_data.ShowDialog();
    }
}

}

0 个答案:

没有答案