我从学校收到的项目有简单的说明:创建一个工作3表单C#应用程序。我决定创建一个表单,让用户从3种不同的选项中选择(在这种情况下:1张票,2张票和3张票)。然后它将切换到第二种形式,并让用户从3个选项中选择(在这种情况下:1袋爆米花,大苏打和一袋薯条)。我遇到的问题是,当它试图显示总成本时,它总是最终为0.我真的很感激任何帮助 代码:
Form1中:
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;
namespace WongGregory9_part3ThreeForms
{
public partial class movieForm : Form
{
public movieForm()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
//Close form
this.Close();
}
private void displayButton_Click(object sender, EventArgs e)
{
//create a variable named snack for snackForm
snackForm snack = new snackForm();
//show the form snack
snack.ShowDialog();
}
}
窗体2(snackForm):
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;
namespace WongGregory9_part3ThreeForms
{
public partial class snackForm : Form
{
//define varible ticket as 50
int ticket = 50;
//define varible twoTicket as 90
int twoTickets = 90;
//define varible threeTicket as 130
int threeTickets = 130;
//define varible popcorn as 65
int popcorn = 65;
//define varible soda as 30
int soda = 30;
//define varible chips as 40
int chips = 40;
//define varible ticketCost
int ticketCost;
//define varible snackCost
int snackCost;
//define varible totalCost;
int totalCost;
//create a variable named movie for movieForm
movieForm movie = new movieForm();
public snackForm()
{
InitializeComponent();
}
private void snackForm_Load(object sender, EventArgs e)
{
//if ticketRadioButton is checked then..
if (movie.ticketRadioButton.Checked)
{
//ticketCost = ticket
ticketCost = ticket;
}
//if ticketRadioButton2 is checked then..
if (movie.ticketRadioButton2.Checked)
{
//ticketCost = twoTicket
ticketCost = twoTickets;
}
//if ticketRadioButton3 is checked then..
if (movie.ticketRadioButton3.Checked)
{
//ticketCost = threeTicket
ticketCost = threeTickets;
}
//if popcornRadioButton is checked then..
if (popcornRadioButton.Checked)
{
//snackCost = popcorn
snackCost = popcorn;
}
//if sodaRadioButton is checked then..
if (sodaRadioButton.Checked)
{
//snackCost = soda
snackCost = soda;
}
//if chipsRadioButton is checked then..
if (chipsRadioButton.Checked)
{
//snackCost = chips
snackCost = chips;
}
}
private void button1_Click(object sender, EventArgs e)
{
//close form
this.Close();
}
private void displayButton_Click(object sender, EventArgs e)
{
//create a variable named movie for movieForm
displayForm display = new displayForm();
//totalCosts equals ticketCost plus snackCost
totalCost = ticketCost + snackCost;
//display totalCost to displayLabel
display.displayLabel.Text = totalCost.ToString();
//show the dialog entered into displayForm
display.ShowDialog();
}
}
Form3(displayForm):
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;
namespace WongGregory9_part3ThreeForms
{
public partial class displayForm : Form
{
public displayForm()
{
InitializeComponent();
}
}
答案 0 :(得分:0)
是的,我可以看到两个问题,都在snackForm
:
根据谢尔盖的评论,你正在创建一个新的movieForm实例,它不知道原始movieForm实例上的数据。你需要将原始movieForm中的数据传递给你的snackForm,就像你将snackForm的总费用传递给displayForm一样。
您正在加载表单时检查snackForm中的单选按钮选择,即在用户有机会点击它们之前。将这些检查移至snackForm.displayButton_Click