簡體   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