繁体   English   中英

创建一个3 Form C#应用程序。

[英]Creating a 3 Form C# application.

我从学校收到的一个项目有简单的说明:创建一个可正常工作的3窗体C#应用程序。 我决定创建一个表格,让用户从3个不同的选项中进行选择(在这种情况下:1张票,2张票和3张票)。 然后它将切换到第二种形式,还允许用户从3个选项中进行选择(在这种情况下:1袋爆米花,大苏打和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();
    }
}

是的,我可以看到两个问题,都在snackForm

  1. 根据Sergey的评论,您正在创建一个movieForm的新实例,该实例对原始movieForm实例上的数据一无所知。 您需要将数据从原始的movieForm传递到点心表,就像将总费用从点心表传递到displayForm一样。

  2. 表单加载时(即在用户没有机会单击它们之前),您正在检查SnackForm中的单选按钮选择。 将这些支票移到snackForm.displayButton_Click

暂无
暂无

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

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