第二种形式没有正确显示

时间:2013-10-15 17:22:09

标签: c# winforms forms

我创建了两种不同的表单,一种用于用户选择值,另一种用于显示所有总费用。

我确实声明了另一个类的实例并将两个变量传递给了它。在第二个类中,我有一个构造函数接受参数,然后将它们分配给第二个表单中的变量。

当尝试使用“.ShowDialog();”在第二个上显示结果时方法,表格显示为空白,没有任何内容。

第一种形式:

namespace DormAndMeal
{
    public partial class MainForm : Form
    {
        int ALLENPRICE = 1500;
        int PIKEPRICE = 1600;
        int FARTHINGPRICE = 1800;
        int UNIVERSITYPRICE = 2500;

        int MEAL7 = 600;
        int MEAL14 = 1200;
        int UNLIMITEDMEAL = 1700;



        public MainForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // close
            this.Close();
        }

        private void btnOkay_Click(object sender, EventArgs e)
        {

            int roomCharge = 0;
            int foodCharge = 0;

            // Checking for the room plan chosen, assigning price to roomCharge
            if (radAllen.Checked)
            {
                roomCharge = ALLENPRICE;
            }
            else if (radPike.Checked)
            {
                roomCharge = PIKEPRICE;
            }
            else if (radFarthing.Checked)
            {
                roomCharge = FARTHINGPRICE;
            }
            else if (radUniversity.Checked)
            {
                roomCharge = UNIVERSITYPRICE;
            }


            // Checking for which meal plan was selected, assign price to foodCharge
            if (radMeal1.Checked)
            {
                foodCharge = MEAL7;
            }
            else if (radMeal2.Checked)
            {
                foodCharge = MEAL14;
            }
            else if (radMeal3.Checked)
            {
                foodCharge = UNLIMITEDMEAL;
            }

            // Instance of Charges form
            Charges myCharges = new Charges(foodCharge, roomCharge);

            // Display the form
            myCharges.ShowDialog();
        }
    }
}

如您所见,表单会找到选中的单选按钮并相应地分配价格,然后创建第二个表单的实例并显示它。

表格2:

namespace DormAndMeal
{
    public partial class Charges : Form
    {
        int foodTotal;
        int roomTotal;

        public Charges()
        {
            InitializeComponent();
        }

        public Charges(int food, int room)
        {
            this.roomTotal = room;
            this.foodTotal = food;
        }

        private void Charges_Load(object sender, EventArgs e)
        {
            lblFood.Text = foodTotal.ToString("c");
            lblRoom.Text = roomTotal.ToString("c");

            lblTotal.Text = (foodTotal + roomTotal).ToString("c");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            // close
            this.Close();
        }
    }
}

这个表单有一个构造函数来接受参数并分配它们,然后遍历并将它们显示给位于表单上的相应变量。

由于某种原因,第二种形式显示为空白。

1 个答案:

答案 0 :(得分:4)

您没有调用InitializeComponents方法,该方法将从设计器文件中创建所有控件:

public Charges(int food, int room)
{
  InitializeComponent();
  this.roomTotal = room;
  this.foodTotal = food;
}

或者在评论中指出,你可以调用你的其他构造函数:

public Charges()
{
  InitializeComponent();
}

public Charges(int food, int room)
  : this()
{
  this.roomTotal = room;
  this.foodTotal = food;
}