C# 将输入数据从一个表单传递到另一个表单中的标签

C# 将输入数据从一个表单传递到另一个表单中的标签,c#,winforms,label,C#,Winforms,Label,我在一个表单上有一些标签,其目的是保存来自另一个表单的用户输入的值,因此,我需要通过编程而不是使用“属性”窗口来设置标签文本。我已经这样做了,但问题是数据似乎没有传递到另一种形式。我尝试运行调试,但解决方案锁定在我身上,我必须从任务管理器关闭它。我在打开第二个表单的方法上设置了断点。我的想法是,当程序通过该方法时,我可以看到属性中是否填写了正确的信息。不幸的是,它没有单步执行该方法,而是打开表单并锁定 我希望有人能看看我的代码,告诉我哪里出了问题 用户输入所有数据后,他/她单击一个按钮将其保存到

我在一个表单上有一些标签,其目的是保存来自另一个表单的用户输入的值,因此,我需要通过编程而不是使用“属性”窗口来设置标签文本。我已经这样做了,但问题是数据似乎没有传递到另一种形式。我尝试运行调试,但解决方案锁定在我身上,我必须从任务管理器关闭它。我在打开第二个表单的方法上设置了断点。我的想法是,当程序通过该方法时,我可以看到属性中是否填写了正确的信息。不幸的是,它没有单步执行该方法,而是打开表单并锁定

我希望有人能看看我的代码,告诉我哪里出了问题

用户输入所有数据后,他/她单击一个按钮将其保存到数据库,并显示一条消息,要求用户单击另一个按钮转到另一个表单。这部分很好用。以下是该操作的事件处理程序代码:

private void btnEnterNutritionInfo_Click(object sender, EventArgs e)
{
    //call methods from NutritionBOL class to input calculated 
    //data into textboxes
    this.txtRMR.Text = Convert.ToString(busObject.GetRMRdata(
        busObject.BodyWeight));
    this.txtDailyActivityBurn.Text = Convert.ToString(
        busObject.GetDailyActivityBurnData(
        busObject.RestingMetabolicRate));
    this.txtEnergyAmount.Text = Convert.ToString(
        busObject.GetEnergyAmount(
        busObject.RestingMetabolicRate, busObject.DailyActivityBurn));
    this.txtYourNutritionLevel.Text = busObject.GetNutritionLevel(
        busObject.DailyEnergyAmount);

    //convert text box data and assign to variables
    busObject.BodyFatStart = double.Parse(this.txtBodyFatStart.Text);
    busObject.BodyFatEndOfPhase1 = double.Parse(this.txtBodyFatEndOfPhase1.Text);
    busObject.BodyFatEndOfPhase2 = double.Parse(this.txtbodyFatEndOfPhase2.Text);
    busObject.BodyFatEndOfPhase3 =  double.Parse(this.txtbodyFatEndOfPhase3.Text);             
    busObject.BodyWeight = double.Parse(this.txtBodyWeight.Text);
    busObject.RestingMetabolicRate = double.Parse(this.txtRMR.Text);
    busObject.DailyActivityBurn = double.Parse(this.txtDailyActivityBurn.Text);
    busObject.DailyEnergyAmount = double.Parse(this.txtEnergyAmount.Text);
    busObject.BodyFatStartNotes = this.txtStartNotes.Text;
    busObject.BodyFatEndOfPhase1Notes = this.txtBodyFatEndOfPhase1Notes.Text;
    busObject.BodyFatEndOfPhase2Notes = this.txtBodyFatEndOfPhase2Notes.Text;
    busObject.BodyFatEndOfPhase3Notes = this.txtBodyFatEndOfPhase3Notes.Text;
    busObject.NutritionLevel = this.txtYourNutritionLevel.Text;  


    //call method to save input data
    busObject.SaveData();

    //set visibility to true
    txtRMR.Visible = true;
    txtDailyActivityBurn.Visible = true;
    txtEnergyAmount.Visible = true;
    txtYourNutritionLevel.Visible = true;
    lblYourNutritionLevel.Visible = true;
}
完成后,用户单击按钮转到另一个表单。此时,标签数据应该传递给另一个表单。另一个表单打开,但标签数据未填充:以下是该按钮单击的事件处理程序:

private void btnMeasurementData_Click(object sender, EventArgs e)
{
    //assign text boxes to properties 
    this.BodyFatB4 = this.txtBodyFatStart.Text;
    this.BodyWeightB4 = this.txtBodyWeight.Text;
    this.BodyFatAfter = this.txtBodyFatEndOfPhase1.Text;
    this.BodyWeightAfter = this.txtBodyWeight.Text;

    //instantiate an object from the MeasurementsBOL to pass data
    MeasurementsForm measurementsForm = new MeasurementsForm();
    measurementsForm.BodyFatB4 = this.BodyFatB4;
    measurementsForm.BodyWeightB4 = this.BodyWeightB4;
    measurementsForm.BodyFatAfter = this.BodyFatAfter;
    measurementsForm.BodyWeightAfter = this.BodyWeightAfter;

    //StartDate data is just being passed through to the 
    //MeasurementsForm from the FitnessTest form
    measurementsForm.StartDate = this.StartDate;            

    this.Hide();

    //method call to open MeasurementsForm
    busObject.GoToMeasurementsForm(this.StartDate, this.BodyFatB4,
        this.BodyWeightB4, this.BodyFatAfter, this.BodyWeightAfter);
} 
以下是打开MeasurementsForm的方法调用代码:

public void GoToMeasurementsForm(string startDate, string bodyFatB4,
    string bodyWeightB4, string bodyFatAfter, string bodyWeightAfter)
{
    MeasurementsForm f2;
    //logic to decide what to do if an instance is not already instanitated
    if (Application.OpenForms["MeasurementsForm"] == null)
    {
        f2 = new MeasurementsForm();
        f2.StartDate = startDate;
        f2.BodyFatB4 = bodyFatB4;
        f2.BodyWeightB4 = bodyWeightB4;
        f2.BodyFatAfter = bodyFatAfter;
        f2.BodyWeightAfter = bodyWeightAfter;
        f2.Name = "MeasurementsForm";
    }
    //logic to decide what to do if an instance is not already instantiated
    else
    {
        f2 = Application.OpenForms["MeasurementsForm"] as MeasurementsForm;
        f2.StartDate = startDate;
        f2.BodyFatB4 = bodyFatB4;
        f2.BodyWeightB4 = bodyWeightB4;
        f2.BodyFatAfter = bodyFatAfter;
        f2.BodyWeightAfter = bodyWeightAfter;
    }
        f2.Show();
}  
这是单击按钮时打开的表单的代码:

public partial class MeasurementsForm : Form
{
    //declare variables
    string bodyFatB4 = "",
           bodyWeightB4 = "",
           bodyFatAfter = "",
           bodyWeightAfter = "",
           startDate = "";

    //instantiate object from MeasurementsBOL class
    MeasurementsBOL busObject = new MeasurementsBOL();        

    //default constructor
    public MeasurementsForm()
    {
        InitializeComponent();
        busObject.InitializeConnection();
    }

    //properties for variables
    public string BodyFatB4
    {
        get { return bodyFatB4; }
        set { bodyFatB4 = value; }
    }

    public string BodyWeightB4
    {
        get { return bodyWeightB4; }
        set { bodyWeightB4 = value; }
    }

    public string BodyFatAfter
    {
        get { return bodyFatAfter; }
        set { bodyFatAfter = value; }
    }

    public string BodyWeightAfter
    {
        get { return bodyWeightAfter; }
        set { bodyWeightAfter = value; }
    }

    public string StartDate
    {
        get { return startDate; }
        set { startDate = value; }
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        //pass text data from nutrition form to labels
        this.lblBodyFatB4FromNutrition.Text = this.BodyFatB4;
        this.lblWeightB4FromNutrition.Text = this.BodyWeightB4;
        this.lblBodyFatAfterFromNutrition.Text = this.BodyFatAfter;
        this.lblWeightAfterFromNutrition.Text = this.BodyWeightAfter;
    }

    //event handler for B4 input data
    private void btnEnterMeasurementsB4_Click(object sender, EventArgs e)
    {
        //convert input data and assign to variables
        busObject.BodyFatB4 = double.Parse(lblBodyFatB4FromNutrition.Text);
        busObject.BodyWeightB4 = double.Parse(lblWeightB4FromNutrition.Text);
        busObject.ChestMeasurementB4 = double.Parse(txtChestB4.Text);
        busObject.WaistMeasurementB4 = double.Parse(txtWaistB4.Text);
        busObject.HipsMeasurementB4 = double.Parse(txtHipsB4.Text);
        busObject.RightThighB4 = double.Parse(txtRightThighB4.Text);
        busObject.LeftThighB4 = double.Parse(txtLeftThighB4.Text);
        busObject.RightArmB4 = double.Parse(txtRightArmB4.Text);
        busObject.LeftArmB4 = double.Parse(txtLeftArmB4.Text);

        //call method to save input data
        busObject.SaveB4Data();

        //clear text boxes of data
        this.txtChestB4.Clear();
        this.txtWaistB4.Clear();
        this.txtHipsB4.Clear();
        this.txtRightThighB4.Clear();
        this.txtLeftThighB4.Clear();
        this.txtRightArmB4.Clear();
        this.txtLeftArmB4.Clear();

        //close form
        this.Close();            
    }

    //event handler for after input data 
    private void btnEnterMeasurementsAfter_Click(object sender, EventArgs e)
    {
        //convert input data and assign to variables      
        busObject.BodyFatAfter = double.Parse(lblBodyFatAfterFromNutrition.Text);
        busObject.BodyWeightAfter = double.Parse(lblWeightAfterFromNutrition.Text);
        busObject.ChestMeasurementAfter = double.Parse(txtChestAfter.Text);
        busObject.WaistMeasurementAfter = double.Parse(txtWaistAfter.Text);
        busObject.HipMeasurementAfter = double.Parse(txtHipsAfter.Text);
        busObject.RightThighAfter = double.Parse(txtRightThighAfter.Text);
        busObject.LeftThighAfter = double.Parse(txtLeftThighAfter.Text);
        busObject.RightArmAfter = double.Parse(txtRightArmAfter.Text);
        busObject.LeftArmAfter = double.Parse(txtLeftArmAfter.Text);

        //call method to save input data
        busObject.SaveAfterData();

        //clear text boxes of data
        this.txtChestAfter.Clear();
        this.txtWaistAfter.Clear();
        this.txtHipsAfter.Clear();
        this.txtRightThighAfter.Clear();
        this.txtLeftThighAfter.Clear();
        this.txtRightArmAfter.Clear();
        this.txtLeftArmAfter.Clear();

        //close form
        this.Close();
    }

    //event handler to open schedule form and hide this form
    private void btnClickForSchedule_Click(object sender, EventArgs e)
    {
        this.Hide();
        busObject.BackToMainSchedule(this.StartDate);
    }
}

@CodeBlend-我在打开第二个表单的方法上设置了断点。我的想法是,当程序通过该方法时,我可以看到属性中是否填写了正确的信息。不幸的是,它没有单步执行该方法,而是打开表单并锁定(如果它锁定),然后使用console.writeline打印出一些行,您可以在输出窗口中看到这些行,并检查它打印消息的位置。特别是在MeasurementsForm构造函数和显示表单的代码中放置一些消息。我建议使用一些应用程序级变量并简化代码