Kevin.A
Kevin.A

Reputation: 77

Adding collection to listbox

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 is the form.

RecipeManager is a collection manager class for Recipe

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

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

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

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

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

Here is how I tried it:

The form 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 RecipeManagerclass (This one is empty because it inherits its methods from 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);
        }
    }

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

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

Upvotes: 1

Views: 2104

Answers (3)

anhtv13
anhtv13

Reputation: 1818

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

Upvotes: 1

anhtv13
anhtv13

Reputation: 1818

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

Your recept Object has no value.

My solution:

-In Recipe class: Add a constructor to Recipe object

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

-In RecipeManager class: add method:

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

and change "T" to "Recipe" object.

-In FoodRegister class: Add code to method

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

Hope that's useful. Let me know when it's done.

Upvotes: 1

feiyun0112
feiyun0112

Reputation: 771

You need implement ToString() for ListManager

public override string ToString()
    {
        return string.Join(",",this.m_list.ToArray());
    }

Upvotes: 0

Related Questions