gpt4 book ai didi

c# - NullReferenceException 将输入发送到 TextBox

转载 作者:行者123 更新时间:2023-11-30 22:24:02 25 4
gpt4 key购买 nike

我正在尝试将一个字符串从 form2 发送到 form3 以使用信息预加载表单。我在 form3 上调用一个方法,接受启动 linq 查询的字符串,以在表单上设置控件。我得到一个 NullReferenceException was unhandled in the first foreach on recipeNameTextBox.Text = 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

关于c# - NullReferenceException 将输入发送到 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13002962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com