将集合添加到列表框

2024-04-18

我正在尝试让用户能够添加name and ingredient到列表框。

下面简单介绍一下相关类:

RecipeForm是形式。

RecipeManager是一个集合管理器类Recipe

Recipe是“内容”类,其中字段name and ingredients are

ListManager是通用基类,其中管理器类RecipeManager继承自.

我希望用户输入名称和成分recipe object.

然后将该对象保存在RecipeManager in the RecipeForm class.

名单Recipe保存在集合中的对象(m_foodManager在下面的代码中)应该显示在列表框中RecipeForm

这是我尝试的方法:

表格FoodRegister:

 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.Name = Nametxt.Text;
            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);
          }
     }

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

            m_foodmanager.Add(recept);

            ReadValues();
            MessageBox.Show(m_food.Ingredients.ToString());
            UpdateResults();
     }
}

Recipe class

public class Recipe
    {
        private ListManager<string> m_ingredients = new ListManager<string>();
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

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

        public override string ToString()
        {
            return string.Format("{0}  {1}",this.Name, this.Ingredients);
        }
    }

The RecipeManager类(这个是空的,因为它继承了它的方法ListManager.:

 public class RecipeManager : ListManager<Recipe>
    {
        public RecipeManager()
        {
        }
    }

The ListManager class:

  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);
        }
    }

问题是当我输入名称和成分后单击“添加”按钮时,列表框显示:

[命名空间].ListManager`1[System.String]


Recipe recept = new Recipe();
m_foodmanager.Add(recept);

您的接收对象没有价值。

我的解决方案:

-在Recipe类中:向Recipe对象添加构造函数

public Recipe(string name){
this.name = name;
}

-在RecipeManager类中:添加方法:

public List<Recipe> AddList(Recipe r){
return m_list;
}

并将“T”更改为“Recipe”对象。

-在FoodRegister类中:向方法添加代码

private void Addbtn_Click(object sender, EventArgs e){
Recipe rc = new Recipe(Nametxt.Text);
ListManager lm = new ListManager();
lm.Add(rc);
for (int index = 0; index < lm.Count; index++)
      {  
            Recipe recipe = lm.GetAt(index);
            ResultListlst.Items.Add(recipe.Name);
      }
}

希望这有用。 完成后请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将集合添加到列表框 的相关文章

随机推荐