我正在努力让用户能够将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);
}
}
RecipeManager
类(此类为空,因为它从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]
答案 0 :(得分:1)
Recipe recept = new Recipe();
m_foodmanager.Add(recept);
您的信箱对象没有价值。
我的解决方案:
-In Recipe类:向Recipe对象添加构造函数
public Recipe(string name){
this.name = name;
}
-Re RecipeManager类:添加方法:
public List<Recipe> AddList(Recipe r){
return m_list;
}
并将“T”更改为“Recipe”对象。
-In 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);
}
}
希望这很有用。 让我知道它何时完成。
答案 1 :(得分:1)
食谱类
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类:
//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类
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();
}
答案 2 :(得分:0)
您需要为ListManager实现ToString()
public override string ToString()
{
return string.Join(",",this.m_list.ToArray());
}