【问题标题】:Adding collection to listbox将集合添加到列表框
【发布时间】: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


【解决方案1】:
Recipe recept = new Recipe();
m_foodmanager.Add(recept);

您的接收对象没有价值。

我的解决方案:

-在Recipe类中:向Recipe对象添加构造函数

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

-在RecipeManager 类中:添加方法:

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

并将“T”更改为“Recipe”对象。

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

希望这很有用。 完成后告诉我。

【讨论】:

  • 它部分起作用了。它只显示名称。我也想让它显示成分。谢谢
  • 非常简单:为配方对象再定义一个变量并将其添加到列表框:ResultListlst.Items.Add(recipe.Name + " " + recipe.Ingredient);希望这很有用。
  • 我试过了,但我不知道如何将列表ListManager&lt;string&gt; 分配给文本框。请您在代码中显示示例。
  • 如果你想同时显示名称和成分,你的 ListManager 不能这样做。因为您需要显示 2 个字符串,而您的列表只有 1 个。所以我的解决方案是您将 ListManager 更改为 ListManager。
  • 如果我将ListManager&lt;string&gt; 更改为ListManager&lt;Recipe&gt; 会有什么帮助?问题是我不知道如何将成分文本框分配给`ListManager`
【解决方案2】:

食谱类

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

【讨论】:

  • 如果我只想展示成分?
  • ListManager 包含您的食谱对象(名称、成分等)。 ListBox 是显示你想要显示的内容。所以,你的问题只是展示成分,我的回答是没有改变配方对象。您更改列表框代码:ResultListlst.Items.Add(recipe.Ingredient);
【解决方案3】:

你需要为 ListManager 实现 ToString()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多