簡體   English   中英

列表框顯示的是類名而不是值

[英]Listbox is displaying class name instead of values

我正在開發一個項目,其中用戶應該向動物輸入值(名稱,年齡,性別等),並且用戶輸入的值應該顯示在列表框上。這些類相互繼承。 以下是繼承的工作原理:

Animal類是所有類的父級。

哺乳動物類繼承自Animal類。

類繼承自哺乳動物類。

Cat類繼承自Mammal

爬蟲類繼承自Animal

Snake類繼承自Reptile

Lizard類繼承自Reptile

用戶可以選擇要創建的動物。 有一個列表框顯示動物的類型(哺乳動物和爬行動物),旁邊有一個列表框,根據用戶選擇的動物類型,它會顯示動物。

例如,如果用戶在列表框中選擇“ 哺乳動物 ”,則其旁邊的列表框將顯示“ 狗”和“ 貓”

這是完美的工作,這不是問題。

問題是,當用戶在列表框中選擇動物類型(例如MammalDog )后輸入動物值(名稱和年齡)並單擊添加按鈕時,結果列表框會在嘗試添加哺乳動物時顯示Assign1_1.Mammal動物和Assign_1.Reptile在嘗試添加爬行動物時。

我想要它做的是在結果列表框中顯示用戶輸入的值(名稱和年齡),而只是根據所選動物顯示Assign1.MammalAssign1.Reptile

這是我的MainForm代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assign_1
{
    public partial class MainForm : Form
    {
        private Dog m_dog = new Dog();
        private Cat m_cat = new Cat();
        private Snake m_snake = new Snake();
        private Lizard m_lizard = new Lizard();
        private AnimalManager animalmgr  = null;
        private Animal m_animal = new Animal();
        public MainForm()
        {

            //Visual Studio initializations
            InitializeComponent();

            //My initializations
            InitializeGUI();
            Gendercmb.DataSource = Enum.GetValues(typeof(GenderType));
            Categorylst.DataSource = Enum.GetValues(typeof(Categorytype));
            animalmgr = new AnimalManager();

        }

        private void InitializeGUI()
        {
            ReadInput();
        }

        private void ReadInput()
        {
            m_animal.Name = ReadName();
            m_animal.Age = ReadAge();
        }
        private int ReadAge()
        {
            int age = 0;

            int.TryParse(Agetxt.Text, out age);

            return age;
        }

        private string ReadName()
        {
            string name = "";
            name = Nametxt.Text;
            return name;
        }

        private void addMammal(Mammal values)
        {

            ReadInput();

            switch ((MammalType)Animallst.SelectedIndex)
            {
                 case MammalType.Dog:
                    {

                        // Use a copy constructor to set a dog with common data
                        Dog m_dog = new Dog(values);
                        // If more data in GUI to fill in for this animal, do it here
                        //Then send it to the manager for adding to the list
                        animalmgr.add(m_dog);
                        break;
                   } 
                case MammalType.Cat:
                    {
                        Cat m_cat = new Cat();
                        animalmgr.add(m_cat);
                        break;
                    }
            }
        }

        private void AddReptile(Reptile values)
        {
            ReadInput();

            switch ((ReptileType)Animallst.SelectedIndex)
            {
                case ReptileType.Snake:
                    {

                        // Use a copy constructor to set a snake with common data
                        Snake m_snake = new Snake(values);
                        // If more data in GUI to fill in for this animal, do it here
                        //Then send it to the manager for adding to the list
                        animalmgr.add(m_snake);
                        break;
                    }
                case ReptileType.Lizard:
                    {
                        Lizard m_lizard = new Lizard();
                        animalmgr.add(m_lizard);
                        break;
                    }
            }
        }

        //When user clicks "Add to list"
        private void button1_Click(object sender, EventArgs e)
        {
            ReadInput();

            switch ((Categorytype)Categorylst.SelectedIndex)
            {
                case Categorytype.Mammal:
                    {
                        Mammal mammal = new Mammal(m_animal);
                        addMammal(mammal);
                        break;
                    }
                case Categorytype.Reptile:
                    {
                        Reptile m_reptile = new Reptile(m_animal);
                        AddReptile(m_reptile);
                        break;
                    }


            }
            UpdateResults();
        }
        private void UpdateResults()
        {

            Resultlst.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 < animalmgr.ElementCount; index++)
            {
                Animal animal = animalmgr.GetElementAtPosition(index);

                Resultlst.Items.Add(animal.ToString());
            }
        }

這是我的Animal經理課程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assign_1
{
    class AnimalManager
    {
        private List<Animal> m_animal;
        private List<Mammal> m_mammal;
        public AnimalManager()
        {
            //In this list objects off diff animals of all species are saved
            m_animal = new List<Animal>();
            m_mammal = new List<Mammal>();
        }


        public void add(Animal ObjIn)
        {
            m_animal.Add(ObjIn);
        }

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

        public Animal GetElementAtPosition(int index)
        {
            //We choose to return a copy, why do I need type casting when copying?
        if (IsIndexValid(index))
            {
                if (m_animal[index] is Mammal)
                    return new Mammal((Mammal)m_animal[index]);
                if (m_animal[index] is Reptile)
                    return new Reptile((Reptile)m_animal[index]);
                return null;
            }
            else
                return null;
        }

        public int ElementCount
        {
            get { return m_animal.Count; }
        }


    }
}

這是我的哺乳動物課:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assign_1
{
    class Mammal : Animal
    {
        private MammalType theMammal;
        private int teeth;


        public Mammal() : base()
             {

             }
        public Mammal(Animal other)
        {
            this.Name = other.Name;
            this.Age = other.Age;
            this.Gender = other.Gender;
            this.Id = other.Id;

        }

        public Mammal (MammalType type)
        {
            theMammal = type; 
        }

#region Props
        public MammalType TheMammal
        {
            get { return theMammal; }
            set { theMammal = value; }
        }


        public int Teeth
        {
            get { return teeth; }
            set { teeth = value; }
         }
#endregion


    }
}

這是我的Dog課程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assign_1
{
    class Dog : Mammal
    {


        public Dog()
        {

        }
      public Dog(Mammal other)
    {
      this.Name = other.Name;
      this.Age = other.Age;
      this.Teeth = other.Teeth;
    }


    }
}

編輯為了做到這一點,我只展示了兒童類哺乳動物 如果我能弄清楚如何創建一只 ,我也可以修復其他

您需要覆蓋ToString() - 方法。 請注意簽名中的override關鍵字。

public override string ToString()
{
    //You can put anything here and it will be returned by the ToString()-method.
    return this.Name + ", " + this.Age;
}

將此代碼放入Dog -class中。 它將使dog.ToString()返回Name, Age

此外,您實際上可以將此覆蓋放在您的基類Animal中,它將被轉移到所有子類。 只要它們不覆蓋ToString()本身,它們也將返回Name, Age

您只需要在類中添加ToString()方法的覆蓋。
覆蓋返回您選擇在ListBoxes中顯示的值。

ListBox在准備好顯示項目時,使用添加到其items集合的類實例的ToString方法。 如果沒有ToString(),則使用基類Object.ToString() ,該方法只返回類名

例如,您可以使用更改Dog類

class Dog : Mammal
{
    public Dog()
    {
    }
    public Dog(Mammal other)
    {
       this.Name = other.Name;
       this.Age = other.Age;
       this.Teeth = other.Teeth;
    } 
    public override string ToString()
    {
       return string.Format("{0}, {1}, {2}", 
               this.Name, this.Age, this.Teeth);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM