繁体   English   中英

C#Windows窗体之间的通信错误

[英]Communication Error between C# Windows Forms

我正在尝试制作一个表单,用户将在其中输入值以计算披萨餐厅的订单。 在用户计算一个订单后,他们可以在此之后生成多个订单。 所以,我试图将每个订单添加到一个数组,然后将数组拉出到另一个类,以便我可以查看另一个表单上的所有订单的摘要。 这是我在第一个表单中的代码。 其他类都不能访问任何数组。 你能救我吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace MidTermPizzas
{
    public partial class Form1 : Form
    {
        pizzaOrder aOrder = new pizzaOrder();
        public static ArrayList pizzaAmountArray = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

        //click File, Exit
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Enjoy your pizza!");
            this.Close();
        }

        //click View, Summary of Orders Placed
        private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced();
            myForm.Show();
        }

        //form load
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //text in box to the right of "Amount of Pizzas"
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //ArrayList pizzaAmountArray = new ArrayList();

            int v;
            if (int.TryParse(textBox1.Text, out v))
            {
                aOrder.numberOfPizzas = v;
                pizzaAmountArray.Add(v); //add to array list
            }
            else aOrder.numberOfPizzas = 0;
        }

        //text in box to the right of "Amount of Cokes"
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ArrayList cokeAmountArray = new ArrayList();

            int v;
            if(int.TryParse(textBox2.Text, out v))
            {
                aOrder.numberOfCokes = v;
                cokeAmountArray.Add(v); //add to array list
            }
            else aOrder.numberOfCokes = 0;
        }

        //click Calculate Amount Due
        private void calculateAmountDue_Click(object sender, EventArgs e)
        {
            textBox3.Text = Convert.ToString(aOrder.GetAmountDue());
        }

        //click Calculate Sales Tax
        private void button2_Click(object sender, EventArgs e)
        {
            textBox4.Text = Convert.ToString(aOrder.TaxDue());
        }

        //text in box to the right of "Amount Paid"
        private void textBox5_TextChanged_1(object sender, EventArgs e)
        {
            ArrayList totalAmountPaidArray = new ArrayList();

            double v;
            if (double.TryParse(textBox5.Text, out v))
            {
                aOrder.getAmountPaid = v;
                totalAmountPaidArray.Add(v); //add to array list
            }
            else aOrder.getAmountPaid = 0;
        }

        //click Calculate Change Due button
        private void calculateChangeDue_Click(object sender, EventArgs e)
        {
            textBox6.Text = Convert.ToString(aOrder.GetChangeDue());
        }


        //reset button
        private void button1_Click_1(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
        }

        }

    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace MidTermPizzas
{
    class DailySummary
    {
        Form1 aForm1Stuff = new Form1();
        public int numberOfPizzas
        {
            get
            {
                //not sure what to put here
            }
            set
            {
            }
        }

您的代码存在一些概念性问题。

您不是将数字(v)添加到ArrayList而是添加pizzaOrder,并且您不会每次都对text_changed事件做出反应。 在WinForms中,每个键入的字符都会调用它。 如果您的用户输入错误并在他/她表示2时键入3,则表示您已将数字3添加到数组中。 此外,还有更好的替代使用的ArrayList类....

因此,首先删除全局pizzaOrder并将您的ArrayList更改为List<pizzaOrder>

// pizzaOrder aOrder = new pizzaOrder();
public List<pizzaOrder> pizzasOrdered = new List<pizzaOrder>();

然后添加一个名为InsertOrder的新按钮,并为其click事件添加此代码

private void InsertOrder_Click_1(object sender, EventArgs e)
{
    // Call a common function that creates an object of pizzaOrder 
    // from the values typed by your user and add it to the List
    pizzaOrder aOrder = MakeAnOrder();
    pizzasOrdered.Add(aOrder);
}

private pizzaOrder MakeAnOrder()
{
    double d;
    int v;
    pizzaOrder aOrder = new pizzaOrder();

    // If zero is an acceptable value for pizza/cokes and amount then
    // you don't need to check the outcome of the tryparse...
    int.TryParse(textBox1.Text, out v);
    aOrder.numberOfPizzas = v;
    int.TryParse(textBox2.Text, out v);
    aOrder.numberOfCokes = v;
    double.TryParse(textBox5.Text, out d);
    aOrder.getAmountPaid = d;
    return aOrder;
 }

现在,您可以删除文本框上的事件处理程序,并更改以下事件中的代码

private void calculateAmountDue_Click(object sender, EventArgs e)
{
    textBox3.Text = Convert.ToString(MakeAnOrder().GetAmountDue());
}
.....
// the same code for the other buttons

最后,当您需要将List<pizzaOrder>传递给另一个表单时,只需在第二个表单的构造函数中传递全局变量

private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
    SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced(pizzasOrdered);
    myForm.Show();
}

并在表单的构造函数中接收List

public class SummaryOfOrdersPlaced : Form
{
    public SummaryOfOrdersPlaced(List<pizzaOrder> orders)
    {
        foreach(pizzaOrder o in orders)
           ... use the passed in variable to loop on orders and create your display
    }
}

将一个公共数组属性添加到类#1或类#2,以便从其他类可以看到它

你可以传递你想要在表单之间共享的arraylist实例作为构造函数的参数

您需要做的就是从第二个类中访问ArrayList并在那里实例化该属性,如下所示:

public DailySummary()
{
    SomeArrayListProperty = Form1.PizzaAmounArray;
    //do what you want 
}

private ArrayList SomeArrayListProperty {get; set;}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM