简体   繁体   English

如何在运行时在表单中显示继承自 Picturebox 和其他类的实例?

[英]How do I display an instance that inherits from Picturebox and other classes in a form during runtime?

I have some abstract classes in my program and the mother class inherits from PictureBox.我的程序中有一些抽象类,母亲 class 继承自 PictureBox。 My goal is that when I create an instance of my mother class, this instance should appear as a PictureBox in my form.我的目标是,当我创建我母亲 class 的实例时,该实例应在我的表单中显示为 PictureBox。

I tried to define the properties that would be needed for the PictureBox in the constructor of my Animal class.我试图在我的 Animal class 的构造函数中定义 PictureBox 所需的属性。

Then as a second step I tried to define the picture that should appear in the form in the constructor of my African_bullfrog class.然后作为第二步,我尝试定义应该出现在我的 African_bullfrog class 的构造函数中的图片。 As a last step I tried to display the instance which is a PictureBox after the instantiation.作为最后一步,我尝试在实例化后显示一个 PictureBox 实例。

My problem is that my picture box is not displayed.我的问题是我的图片框没有显示。

This is the code which is relevant for understanding the problem:这是与理解问题相关的代码:

This is my mother class这是我妈妈 class

Here I try to define the property of the PictureBox.这里我尝试定义一下 PictureBox 的属性。

public abstract class Animal : PictureBox
{
    public string name { get; }
    public int age { get; }
    public bool gender { get; }
    public Image img { get; set;  }

    protected Animal(string name, int age, bool gender)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.Name = this.name;
        this.Size = new Size(16, 16);
        this.Location = new Point(100, 100);
    }

This is my Amphibian class这是我的两栖动物 class

public abstract class Amphibian : Animal, ISwim, IWalk
{
    protected Amphibian(string name, int age, bool gender) : base(name, age, gender)
    {
    }

    public void swim()
    {
        Debug.WriteLine("I swam!");
    }

    public void walk()
    {
        Debug.WriteLine("I walked!");
    }
}

This is my Frog class这是我的青蛙 class

public abstract class Frog : Amphibian
{
    protected Frog(string name, int age, bool gender) : base(name, age, gender)
    {
    }
}

This is the class from which an instance should be created这是应该从中创建实例的 class

Here I try to define the picture of the PictureBox.这里我尝试定义PictureBox的图片。

public sealed class African_bullfrog : Frog 
{
    public African_bullfrog(string name, int age, bool gender) : base(name, age, gender)
    {
        this.img = Zoo.Properties.Resources._01__African_Bullfrog;
        this.Image = img;
    }
}

this is my StartFrom class这是我的 StartFrom class

Here I try to display the picture box这里我尝试显示图片框

    public partial class StartForm : Form
{
    List<Type> animals = new List<Type>();
    int amount_of_animals = 0;

    public StartForm()
    {
        InitializeComponent();
        fillAnimals();
    }

    private void btnCreateAnimal_Click(object sender, EventArgs e)
    {
        string selected = comboBoxAnimal.SelectedItem.ToString();
        Debug.WriteLine(selected);
        CreateAnimalForm createAnimalForm = new CreateAnimalForm(selected);
        if (createAnimalForm.ShowDialog(this) == DialogResult.OK)
        {
            Animal animalInstance = new AnimalFactory().CreateInstance(createAnimalForm.animal, createAnimalForm.name, createAnimalForm.age, createAnimalForm.gender);
            animalCounter();
            animalInstance.Show();

        }
    }

My goal is that when I create an instance of my mother class, this instance should appear as a PictureBox in my form.我的目标是,当我创建我母亲 class 的实例时,该实例应在我的表单中显示为 PictureBox。

To add something to a form you need to actually add it.要将某些内容添加到表单中,您需要实际添加它。 To add a control to a container you would call myPanel.Controls.Add See Control.Controls .要将控件添加到容器中,您可以调用myPanel.Controls.Add参见Control.Controls If you want this to happen when you create your object you could do this in the constructor, taking the container as a constructor argument.如果您希望在创建 object 时发生这种情况,您可以在构造函数中执行此操作,将容器作为构造函数参数。

I would however argue that this is not a great design, There is the Composition over inheritance principle that suggest that components will be more flexible and reusable when inheritance is reduced.然而,我认为这不是一个很好的设计, inheritance 原则上的组合表明,当 inheritance 减少时,组件将更加灵活和可重用。 It is usually recommended to use one of the patterns to separate the domain model from the UI, like MVVM, MVC, MVP .通常建议使用其中一种模式将域 model 从 UI 中分离出来,例如MVVM、MVC、MVP

A more modern design would be to have a list of all the animals you want to show, and map this to the UI, so that adding or removing animals is reflected list of images in the UI.更现代的设计是列出您想要显示的所有动物的列表,并将 map 发送到 UI,以便添加或删除动物反映在 UI 中的图像列表。 In WPF this is fairly easy, you create an observable collection and some xaml code to bind a listview to this collection .在 WPF 中,这相当容易,您创建一个可观察的集合和一些 xaml 代码来将列表视图绑定到此集合 In winforms you will have to do some of this work yourself instead.在 winforms 中,您将不得不自己完成其中的一些工作。

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

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