将集合添加到列表框 [英] Adding collection to listbox

查看:38
本文介绍了将集合添加到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用户能够将 nameingredient 添加到列表框.

I'm trying to make it so that a user is able to add name and ingredient to a listbox.

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

Here is a brief description of the relevant classes:

RecipeForm 是表格.

RecipeManagerRecipe

Recipe 是内容"字段nameingredients 所在的类

Recipe is the "content" class where the fieldsname and ingredients are

ListManager 是管理器类RecipeManager 继承自的通用基类.

ListManageris the generic base class in which the manager classRecipeManager inherits from.

我希望用户输入 recipe 对象的名称和成分.

I want the user to input the name and ingredients of a recipe object.

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

And then save that object in an object of the RecipeManager in the RecipeForm class.

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

The list of Recipe objects saved in the collection (m_foodManager in the code below) is supposed to be displayed in a listbox in the 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();
     }
}

配方

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

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

The RecipeManagerclass (This one is empty because it inherits its methods from ListManager.:

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

ListManager 类:

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

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

The problem is that when I'm clicking the Add button after inputting the name and ingredients, the listbox displays:

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

[namespace].ListManager`1[System.String]

推荐答案

Recipe class

Recipe class

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

private string ingredient;
    public string Ingredient
    {
        get { return ingredient; }
        set { ingredient = value; }
    }
public Recipe(string name, string ingredient){
this.name = name; 
this.ingredient = ingredient;
}

ListManager 类:

ListManager class:

//define a list that contains Recipe Object
private List<Recipe> reList ;

public ListManager(){
reList = new List<recipe>();
}

//add your methods Add, GetAt and Count... here.

FoodRegister 类

FoodRegister class

public partial class FoodRegister : Form
{
ListManager<Recipe> lm;

public FoodRegister()
{
   InitializeComponent();
   lm = new ListManager<Recipe>();
}

private void UpdateResults()
 {
        ResultListlst.Items.Clear();  //Erase current list
        for (int index = 0; index < lm.Count; index++)
      {  
            Recipe recipe = lm.GetAt(index);
            ResultListlst.Items.Add(recipe.Name + " " + recipe.Ingredient);
      }
 }

private void Addbtn_Click(object sender, EventArgs e)
 {
        Recipe recept = new Recipe(txtName.Text, txtIngredient.Text);
        lm.Add(recept);          
        UpdateResults();
 }

这篇关于将集合添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆