NullReferenceException将输入发送到TextBox

时间:2012-10-21 22:46:41

标签: c# textbox

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

1 个答案:

答案 0 :(得分:3)

您可能错过了Form3构造函数中的InitializeComponent