C# 简单列表显示的问题

标签 c# winforms listbox ienumerable

我正在制作一个动物收容所应用程序,我必须在其中创建猫或狗,然后创建它并将其添加到列表中。我有一个按钮应该显示列表中的所有动物,另一个按钮应该显示所有狗。当我单击这两个按钮中的一个时,只会出现最后创建的动物。你能给我一些指导吗?

    namespace AnimalShelter
    {
    class AnimalShelter
    {
    private string Name;
    private int TelNumber;
    private List<Animal> AnimalList;

    public AnimalShelter(string name, int telnumber)
    {
        this.AnimalList = new List<Animal>();
        this.Name = name;
        this.TelNumber = telnumber;
    }

    public Animal FindAnimal(string id)
    {
        foreach (Animal anim in this.AnimalList)
        {
            if (id == anim.ChipRegistrationNumber)
            {
                return anim;
            }
        }
        return null;
    }



    public bool RemoveAnimal(string id)
    {
        if (id.Length <= 0)
            return false;
        Animal ao = this.FindAnimal(id);
        if (ao == null)
            return false;
        this.AnimalList.Remove(ao);
        return true;
    }

    public bool AddNewAnimal(Animal ao)
    {
        if (ao == null)
            return false;

        if (this.FindAnimal(ao.ChipRegistrationNumber) != null)
            return false;

        this.AnimalList.Add(ao);

        return true;
    }




    public List<Dog> GetAllDogs()
    {
       List<Dog> temp = new List<Dog>();
       foreach (Animal animal in this.AnimalList)
       {
           if (animal.GetType() == typeof(Dog))
               temp.Add((Dog)animal);

       }
       return temp;
    }

    public List<Cat> GetAllCats()
    {
        List<Cat> temp = new List<Cat>();
        foreach (Animal animal in this.AnimalList)
        {
            if (animal.GetType() == typeof(Cat))
                temp.Add((Cat)animal);
        }
        return temp;
    }

    public List<Animal> GetAllAnimals()
    {
        return AnimalList;
    }

    }
}

这是实际的形式,我在其中创建猫或狗,并具有显示所有动物或显示所有狗的按钮!

   private void AnimalCreation_Click_1(object sender, EventArgs e)
    {
        string name = tbname.Text;
        string number = tbregno.Text;
        DateTime date = this.DateBroughtIn.Value.Date;
        DateTime lastwalked = this.LastWalked.Value.Date;
        string badhabits = tbbadhabbits.Text;

        if (name.Length <= 0)
        {
            MessageBox.Show("A Name Must Be Given");
        }

        if (number.Length <= 0)
        {
            MessageBox.Show(" A number must be given ");
        }

        if (date > DateTime.Now)
        {
            MessageBox.Show("Invalid date, can not be added");
        }
        else
        {
            Animal a = null;
            if (dog.Checked)
            {

                a = new Dog(number, date, name, lastwalked);
                this.MyAnimalShelter.AddNewAnimal(a);
                MessageBox.Show("Dog Successfully added");
            }
            if (cat.Checked)
            {
                if (badhabits.Length <= 0)
                {
                    MessageBox.Show("Bad Habbits must be filled");
                }
                else
                {
                    a = new Cat(number, date, name, badhabits);
                    this.MyAnimalShelter.AddNewAnimal(a);
                    MessageBox.Show("Cat Successfully added");
                }
            }

        }
    }

    private void AllAnimalsShow_Click(object sender, EventArgs e)
    {
        foreach (Animal animal in MyAnimalShelter.GetAllAnimals())
        {
            listBox1.Items.Clear();
            listBox1.Items.Add(animal);
        }
    }

    private void AllDogsShow_Click_1(object sender, EventArgs e)
    {
        foreach (Animal animal in MyAnimalShelter.GetAllDogs())
        {
            listBox1.Items.Clear();
            listBox1.Items.Add(animal);
        }
    }

最佳答案

每次 foreach 迭代都会清除列表,因此对于数据结构中的每只动物,您都会清除列表并添加一只动物。这就是为什么您最终只得到列表中最后一只动物的原因。

你需要这样做:

listBox1.Items.Clear();
foreach (Animal animal in MyAnimalShelter.GetAllAnimals())
{
    listBox1.Items.Add(animal);
}

关于C# 简单列表显示的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15131704/

相关文章:

c# - 为什么 DateTimePicker 在 Leave 事件处理程序中返回过时的值?

winforms - NHibernate Win Forms session 管理

c++ - wxWidgets - 将所有文件夹写入列表框

forms - 如何在 Excel VBA 中声明和设置 ListBox 变量?

javascript - 如何根据id向列表框添加星号

c# - 修改appSettings时性能不佳

c# - 为什么我们需要回调事件?

c# - 无法将文件存储在组合框中并在 WPF 中读取数据、大小

c# - 如何单击 "Yes"按钮以使用代码订购应用程序

c# - 创建 System.Windows.Controls.Image 会引发异常 - 如何使用调度程序实例化它?