我正在尝试让用户能够添加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]