C# 无法添加到集合中

C# 无法添加到集合中,c#,visual-studio-2013,C#,Visual Studio 2013,我正试图让用户能够将配料添加到列表框中 以下是相关类别的简要说明: RecipeForm是表单 RecipeManager是Recipe的集合管理器类 Recipe是包含字段成分的内容类 ListManager是manager类RecipeManager从中继承的通用基类 我希望用户输入配方对象的成分 然后将该对象保存在RecipeForm类中RecipeManager的对象中 下面代码中保存在集合m_foodManager中的配方对象列表应该显示在RecipeForm的列表框中 以下是我如何尝

我正试图让用户能够将配料添加到列表框中

以下是相关类别的简要说明:

RecipeForm是表单

RecipeManager是Recipe的集合管理器类

Recipe是包含字段成分的内容类

ListManager是manager类RecipeManager从中继承的通用基类

我希望用户输入配方对象的成分

然后将该对象保存在RecipeForm类中RecipeManager的对象中

下面代码中保存在集合m_foodManager中的配方对象列表应该显示在RecipeForm的列表框中

以下是我如何尝试的:

食品登记表:

RecipeManager类此类为空,因为它从ListManager继承了其方法:


问题是listbox上没有显示任何内容,而是添加了空字符串。我不确定是否正确地将字符串添加到集合中。我希望用户通过在文本框中输入值向集合中添加字符串

进行更多调试以缩小问题范围。在ToString重载中添加一些调试测试,看看是否有跳出的内容。逐行运行代码以验证函数是否按预期方式调用。您使用的不是m_food,而是Addbtn_Click中的本地配方。去掉m_食物,在ReadValues中添加一个配方参数。@b谢谢!!这在一定程度上起了作用。我确实在列表框上显示了这个字符串,但现在的问题是,当用户向列表框添加另一个字符串时,以前的所有字符串都会变为同一个字符串。我解决了这个问题!谢谢@bstenzel对我的帮助!
 public partial class FoodRegister : Form
  {
        private Recipe m_food = new Recipe();
        private RecipeManager m_foodmanager = new RecipeManager();


 public FoodRegister()
     {
            InitializeComponent();
     }

 private void ReadValues()
     {           
            m_food.Ingredients.Add(Ingredienttxt.Text);
     }

 private void UpdateResults()
     {
            ResultListlst.Items.Clear();  //Erase current list

            //Get one elemnet at a time from manager, and call its 
            //ToString method for info - send to listbox
            for (int index = 0; index < m_foodmanager.Count; index++)
          {  
                Recipe recipe = m_foodmanager.GetAt(index);
                //Adds to the list.
                ResultListlst.Items.Add(recipe);
                ResultListlst.DisplayMember = "Ingredients";
          }
     }

    private void Addbtn_Click(object sender, EventArgs e)
     {
            Recipe recept = new Recipe();

            m_foodmanager.Add(recept);

            ReadValues();
            UpdateResults();
     }
}
public class Recipe
    {
        private ListManager<string> m_ingredients = new ListManager<string>();

        public ListManager<string> Ingredients
        {
            get { return m_ingredients; }
        }

        public override string ToString()
        {
            return string.Format("{0}", this.Ingredients);
        }
    }
 public class RecipeManager : ListManager<Recipe>
    {
        public RecipeManager()
        {
        }
    }
  public class ListManager<T> : IListManager<T>
    {
        protected List<T> m_list;

        public ListManager()
        {
            m_list = new List<T>();
        }

        public int Count
        {
            get { return m_list.Count; }
        }

        public  void Add(T aType)
        {
            m_list.Add(aType);
        }

        public void DeleteAt(int anIndex)
        {
            m_list.RemoveAt(anIndex);
        }

        public bool CheckIndex(int index)
        {
            return ((index >= 0) && (index < m_list.Count));
        }
  
        public T GetAt(int anIndex)
        {
            if (CheckIndex(anIndex))
                return m_list[anIndex];
            else 


                 return default(T);
       }

       public override string ToString()

        {

        if(m_list != null && m_list.Any())
       {
             return string.Join(", ", m_list);
       }

        return string.Empty;
       }
}