我正在尝试将一个字符串从 form2 发送到 form3 以预加载包含信息的表单。我在 form3 上调用一个方法,该方法接受启动 linq 查询以在表单上设置控件的字符串。我得到 NullReferenceException 在 recipeNameTextBox.Text = a.RecipeName; 的第一个 foreach 中未处理 我可以看到查询运行并且信息在 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);
}
}