简体   繁体   English

列表框显示类名而不是值

[英]Listbox is showing class name instead of values

I'm working on a project where the user is able to create different animals. 我正在做一个用户可以创建不同动物的项目。 This is how the inheritance work: 这是继承的工作方式:

Animal class is the parent for all classes. 动物类是所有类的父类。

Mammal class inherit from Animal class. 哺乳动物类继承自动物类。

Dog class inherit from Mammal class. 狗类继承自哺乳动物类。

Cat class inherit from Mammal class 猫类继承自哺乳动物类

Reptile class inherit from Animal class 爬行动物类继承自动物类

Snake class inherit from Reptile class 蛇类继承自爬行动物类

Lizard class inherit from Reptile class 蜥蜴类继承自爬行动物类

The user is able to choose what animal to create. 用户能够选择要创造的动物。 There is one listbox showing the type of animal ( Mammal and Reptile ) and there is a listbox next to it that that depending on the type of animal selected by the user, it displays its animals. 有一个显示动物类型的列表框(“ Mammal和“ Reptile ),旁边有一个列表框,根据用户选择的动物类型,它会显示其动物。

For exampel, if the user selects Mammal in the listbox, the listbox next to it displays Dog and Cat . 例如,如果用户在列表框中选择“ Mammal ”,则其旁边的列表框将显示“ DogCat

The user is then able to select the animal to create and then input some information about that animal and create it to make it show on a result listbox. 然后,用户可以选择要创建的动物,然后输入有关该动物的一些信息并进行创建,以使其显示在结果列表框中。

I also want it to show a food schedule when an animal is selected on the result listbox. 我还希望它在结果列表框中选择动物时显示食物计划。 For example if a user selects a dog(after creating it) on the result listbox, the dog's food schedule is to be displayed on a schedule listbox. 例如,如果用户在结果列表框中选择了一条狗(在创建后),则该狗的食物时间表将显示在时间表列表框中。

The problem is that when I try to select an item on that listbox, the only thing adding to the schedule listbox is class name FoodSchedule 问题是,当我尝试在该列表框中选择一个项目时,唯一添加到计划列表框中的是类名FoodSchedule

Assign.FoodSchedule 分配食物时间表

Here are my classes: 这是我的课程:

There is a FoodSchedule class that works as a class manager. 有一个FoodSchedule类可以用作类管理器。

public class FoodSchedule
    {
       private List<string> foodDescriptionList;

       public FoodSchedule()
       {
           foodDescriptionList = new List<string>();
       }
        public int Count
        {
            get
            {
                if ((foodDescriptionList != null))
                {
                    return foodDescriptionList.Count;
                }
                else
                {
                    return 0;
                }
            }
        }

        public void AddScheduleItem(string item)
        {

            foodDescriptionList.Add(item);
        }


        public string GetFoodSchedule(int index)
        {
           if(ValidateIndex(index))
           {
                return foodDescriptionList[index];
           }
           else
           {
               return null;
           }
        }

        public bool ValidateIndex(int index)
        {
            return ((index >= 0) && (index < foodDescriptionList.Count));
        }

    }
}

The instance variable foodDescriptionList is a collection of string elements where each element is a line of text that describes a part of the feeding schedule. 实例变量foodDescriptionList是字符串元素的集合,其中每个元素都是一行文字,描述了部分喂食时间表。

The info about the schedule is to be contained and filled in an instance of FoodSchedule in for example the Dog class (hard-coded, through an overridden method) 有关时间表的信息将包含在FoodSchedule的实例中,并填充在Dog类中(通过重写的方法进行硬编码)

class Dog : Mammal
    {
        private FoodSchedule foodScedule = new FoodSchedule();


public override FoodSchedule FoodDescription
        {
            get
            {
                return this.foodScedule;
            }
        }
public override string GetSpecies()
        {
            return "Dog";
        }

     public override FoodSchedule GetFoodSchedule()
        {

            foodScedule.AddScheduleItem("Some text");


            return foodScedule;
        }

My mainForm : 我的mainForm

 public partial class MainForm : Form
    {  
        private FoodSchedule m_foodManager = new FoodSchedule();
        private Dog m_dog = new Dog();
  FoodSchedule animal = m_dog.GetFoodSchedule();

                   if (animal != null)
             {
                    //Adds to the list.
                 foodlst.Items.Add(animal.ToString()); //foodlst is the schedule listbox  
              }    
                foodlst.DisplayMember = "FoodDescription";

From your question, I gather that you want to display the food description (list of string) for the selected animal ie Dog 从您的问题中,我收集到您想要显示所选动物(如狗)的食物描述(字符串列表)

To achieve this in your FoodSchedule class add a property 要在您的FoodSchedule类中实现此目的,请添加一个属性

public List<string> FoodDescriptionList 
{
     get
     {
          return foodDescriptionList;
      }
 }

And in your main form you can bind this collection to your listbox 并可以在您的主窗体中将此集合绑定到列表框

if (animal != null)
{
    //Adds to the list.
    foodlst.DataSource = animal.FoodDescriptionList;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM