简体   繁体   中英

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. My goal is that when I create an instance of my mother class, this instance should appear as a PictureBox in my form.

I tried to define the properties that would be needed for the PictureBox in the constructor of my Animal class.

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. As a last step I tried to display the instance which is a PictureBox after the instantiation.

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

Here I try to define the property of the 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

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

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

Here I try to define the picture of the 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

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.

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 . 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.

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. It is usually recommended to use one of the patterns to separate the domain model from the UI, like 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. In WPF this is fairly easy, you create an observable collection and some xaml code to bind a listview to this collection . In winforms you will have to do some of this work yourself instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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