繁体   English   中英

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

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

我的程序中有一些抽象类,母亲 class 继承自 PictureBox。 我的目标是,当我创建我母亲 class 的实例时,该实例应在我的表单中显示为 PictureBox。

我试图在我的 Animal class 的构造函数中定义 PictureBox 所需的属性。

然后作为第二步,我尝试定义应该出现在我的 African_bullfrog class 的构造函数中的图片。 作为最后一步,我尝试在实例化后显示一个 PictureBox 实例。

我的问题是我的图片框没有显示。

这是与理解问题相关的代码:

这是我妈妈 class

这里我尝试定义一下 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);
    }

这是我的两栖动物 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!");
    }
}

这是我的青蛙 class

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

这是应该从中创建实例的 class

这里我尝试定义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;
    }
}

这是我的 StartFrom class

这里我尝试显示图片框

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

        }
    }

我的目标是,当我创建我母亲 class 的实例时,该实例应在我的表单中显示为 PictureBox。

要将某些内容添加到表单中,您需要实际添加它。 要将控件添加到容器中,您可以调用myPanel.Controls.Add参见Control.Controls 如果您希望在创建 object 时发生这种情况,您可以在构造函数中执行此操作,将容器作为构造函数参数。

然而,我认为这不是一个很好的设计, inheritance 原则上的组合表明,当 inheritance 减少时,组件将更加灵活和可重用。 通常建议使用其中一种模式将域 model 从 UI 中分离出来,例如MVVM、MVC、MVP

更现代的设计是列出您想要显示的所有动物的列表,并将 map 发送到 UI,以便添加或删除动物反映在 UI 中的图像列表。 在 WPF 中,这相当容易,您创建一个可观察的集合和一些 xaml 代码来将列表视图绑定到此集合 在 winforms 中,您将不得不自己完成其中的一些工作。

暂无
暂无

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

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