0

我正在尝试制作一个表单,用户将在其中输入值来计算披萨餐厅的订单。用户计算一个订单后,可以进行多个订单。因此,我试图将每个订单添加到一个数组中,然后将该数组拉出到另一个类中,以便我可以在另一个表单上查看所有订单的摘要。这是我的第一种形式的代码。其他类无法访问任何数组。你能帮帮我吗?

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
            {
            }
        }
4

4 回答 4

2

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

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

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

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

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

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
    }
}
于 2013-11-04T22:46:05.943 回答
0

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

于 2013-11-04T22:29:09.527 回答
0

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

于 2013-11-04T22:43:51.070 回答
0

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

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

private ArrayList SomeArrayListProperty {get; set;}
于 2013-11-04T22:46:18.090 回答