我正在尝试从Form2发送一个字符串到Form3,以将信息预加载到表单中。我在form3上调用一个方法,该方法接受字符串,该字符串启动linq查询以设置表单上的控件。我得到一个NullReferenceException在第一个未处理
    在recipeNameTextBox.Text上的foreach = a.RecipeName;
我可以看到查询已运行,​​并且信息位于a.RecipName中。我想我可能会收到此错误,因为尚未绘制控件。任何想法如何解决这个问题? Form2代码在这里:

 private void updateButton_Click(object sender, EventArgs e)
 {
     Form3 Frm3 = new Form3();
     Frm3.Show();

     this.Hide();
     Frm3.takeInputFromForm2(recipeLabel.Text);
}


这里的form3代码:

 public void takeInputFromForm2(string incommingUpdateRecipe)
 {
     Query updateRecipe = new Query();
     IEnumerable<Recipe> newrecipe = updateRecipe.getRecipeInfo(incommingUpdateRecipe);
     foreach (var a in newrecipe)
     {
         recipeNameTextBox.Text = a.RecipeName;
         nationalityTextBox.Text = a.Nationality;
         eventTextBox.Text = a.Event;
         sourceTextBox.Text = a.Source;
         typeTextBox.Text = a.Type;
         ServingsTextBox.Text = a.Servings;
    }
    foreach (var b in newrecipe)
    {
        userRatingTextBox.Text = Convert.ToString(b.UserRating);
        familyRatingTextBox.Text = Convert.ToString(b.FamilyRating);
        healthRatingTextBox.Text = Convert.ToString(b.HealthRating);
        easeOfCookingTextBox.Text = Convert.ToString(b.CookingTime);
        cookingTimeTextBox.Text = Convert.ToString(b.CookingTime);
    }
    foreach (var c in newrecipe)
    {
        ingredientComboBox.Items.Add(c.RecipeIngredient);
    }
}

最佳答案

您可能在Form3构造函数中缺少InitializeComponent

10-05 21:07