【发布时间】:2015-04-09 12:16:01
【问题描述】:
我正在努力让用户能够将name 和ingredient 添加到列表框。
以下是相关类的简要说明:
RecipeForm是表单。
RecipeManager是Recipe的集合管理器类
Recipe是字段name和ingredients所在的“内容”类
ListManager是管理器类RecipeManager继承自的通用基类。
我希望用户输入recipe 对象的名称和成分。
然后将该对象保存在RecipeManager 类中的RecipeForm 对象中。
保存在集合中的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类
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);
}
}
RecipeManagerclass(这个是空的,因为它从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);
}
}
问题是当我在输入名称和成分后点击添加按钮时,列表框显示:
[命名空间].ListManager`1[System.String]
【问题讨论】:
-
ResultListlst是什么?这是什么API?表格? WPF? ASP.NET? -
ResultListlst是列表框。这是 Windows 窗体应用程序
标签: c# visual-studio-2013