这是家庭作业:我有一个由两个winforms和三个类组成的程序。该程序完成它应该为主窗体执行的工作,并将其适当地显示在主窗体的文本框中。除了显示信息外,它还会保存到字符串列表中。数据由订单信息组成。
字符串列表将该信息传递给另一个类中的方法,并将其存储在应该执行的列表中。完成后,我按下一个按钮,打开另一个表格。在这种形式中,我输入一个订单号。应该发生的是,方法将输入的订单号与列表的行号进行比较并返回该信息,然后将其显示在第二个表单的文本框中。
这是应该发生的事情。相反,当是时候将订单号与列表的行号进行比较时,列表数据就消失了,我无法弄清楚原因。这是我的代码:
private void btnPaymentButton_Click(object sender, EventArgs e)
{
amountPaid = double.Parse(this.txtAmountPaid.Text);
orderObject.GetChangeDue(orderObject.TotalAmountDue, amountPaid);
this.txtNumberOfPizzaOrdered.Clear();
this.txtNumberOfCokesOrdered.Clear();
this.txtAmountDue.Clear();
this.txtAmountPaid.Clear();
this.lblYourOrder.Visible = true;
this.rtxtYourOrder.Visible = true;
this.rtxtYourOrder.Text = orderObject.OrderSummary(amountPaid);
//storeOrderObject = new DailySummary(orderObject.OrderSummary(amountPaid));
storeOrderObject = new DailySummary(this.rtxtYourOrder.Text);
}
private void btnDailySummary_Click(object sender, EventArgs e)
{
DailySummaryForm form = new DailySummaryForm();
// this.Visible = false;
form.Show();
}
........
public class DailySummary
{
//declare variables
int numberOfCokes = 0,
numberOfPizzas = 0,
totalOfCokes = 0,
totalOfPizzas = 0,
orderNumberRequest = 0;
string orderFromForm1 = "",
getAllTheOrders = "",
getAnOrder = "";
List<string> pizzaOrderList = new List<string>();
public DailySummary(string orderForm)
{
orderFromForm1 = orderForm;
StoreOrder(orderFromForm1);
}
public DailySummary(int orderRequest)
{
orderNumberRequest = orderRequest;
GetOrder(OrderNumberRequest);
}
public int OrderNumberRequest
{
get
{
return this.orderNumberRequest;
}
}
//store order
public void StoreOrder(string orderFromForm1)
{
pizzaOrderList.Add(orderFromForm1);
}
//get the order
public string GetOrder(int OrderNumberRequest)
{
for (int row = 0; row < pizzaOrderList.Count; row++)
{
if (row == (OrderNumberRequest - 1))
{
getAnOrder = pizzaOrderList[row];
}
}
return getAnOrder;
}
........
public partial class DailySummaryForm : Form
{
int orderNumberRequest = 0;
//instantiate a from object
OrderForm formObject;
DailySummary summaryObject;
public DailySummaryForm()
{
InitializeComponent();
}
private void btnOrderNumberButton_Click(object sender, EventArgs e)
{
orderNumberRequest = int.Parse(this.txtOrderNumber.Text);
summaryObject = new DailySummary(orderNumberRequest);
this.rtxtDisplayOutput.Visible = true;
this.rtxtDisplayOutput.Text = summaryObject.GetAnOrder;
}
答案 0 :(得分:1)
在你的方法btnPaymentButton_Click中你创建了一个DailySummary类的实例,然后在你在DailySummaryForm中创建DailySummary类的新实例时。
这些实例是分开的,因此不会共享相同的值。
由于您不想持久保存数据库,您可能希望:
答案 1 :(得分:0)
DailySummaryForm 正在创建
的新实例DailySummary。
这就是为什么它是空的。