如何让 class 在表单中创建 PictureBox 对象

How can i make a class create a PictureBox object inside a form

我正在尝试创建一个 MainCharacter class,其工作是根据传递的一些参数在 "room" 对象内创建一个 PictureBox每次加载时的房间。

这里是 MainCharacter class 的代码:

namespace VirtualMuseum
{
    class MainCharacter
    {
        string characterName;
        int characterGender;
        bool registeredUser;
        int[] playerPosition;


        // Character constructor
        public MainCharacter(string name, int gender, bool registered, int[] location)
        {
            characterName = name;
            characterGender = gender;
            registeredUser = registered;
            playerPosition = location;
        }

        public void drawCharacter()
        {
            PictureBox playerBox = new PictureBox();
            playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx;
            playerBox.Width = 28;
            playerBox.Height = 32;
            playerBox.Location = new Point(playerPosition[0], playerPosition[1]);
            playerBox.Visible = true;    
        }
    }
}

以及例如在 room1 中创建玩家对象的代码行

MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);

问题是当进入创建玩家对象的特定房间时,在表单内看不到 PictureBox

-----新动作-----

按照您的指示,在房间内使用以下代码class

public Hall()
    {
        playerPosition = new int[] { 350, 400 };

        InitializeComponent();
        //pictureBox2.Parent = pictureBox1;
        MessageBox.Show(playerPosition.ToString());

        MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);

        player1.drawCharacter(this);
}

MainCharacter 中的以下代码 class:

public void drawCharacter(Form form)
    {
        PictureBox playerBox = new PictureBox();
        playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx;
        playerBox.Width = 28;
        playerBox.Height = 32;
        playerBox.Location = new Point(playerPosition[0], playerPosition[1]);

        // Add the pictureBox to the selected form
        form.Controls.Add(playerBox);
}

虽然我在 drawCharacter 方法中定义了图片框的大小,但我设法在表单中画了一些东西,但它看起来像一条非常小的线。

选项 1
拥有您的表单实例,在 drawCharacter 的末尾添加此代码:

formInstance.Controls.Add(playerBox);

例如:

public void drawCharacter(Form formInstance)
{
    PictureBox playerBox = new PictureBox();
    // Set properties ...

    formInstance.Controls.Add(playerBox);
}

然后在你的表单中,当你需要调用这个方法时使用:

 var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
 player1.drawCharacter(this);

选项2
您可以将方法更改为 return a PictureBox:

public PictureBox drawCharacter()
{
    PictureBox playerBox = new PictureBox();
    // Set properties ...

    return  playerBox;
}

然后在你的表单中,当你需要调用这个方法时使用:

var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
this.Controls.Add(player1.drawCharacter());

要点:

  • 如 "Ivan Stoev" 评论中所述,所有控件都必须添加到 Controls 表单集合。
  • 不要忘记将位置 属性 设置为合适的位置或更好的选项,使用 FlowLayoutPanel 并将 PictureBox 添加到其中。例如,如果您从 (flowLayoutPanel1) 上放置一个 FlowLayoutPabel,您可以使用选项 2 并以这种方式向其添加图片框

使用 FlowLayoutPanel 的代码:

var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
this.flowLayoutPanel1.Controls.Add(player1.drawCharacter());

您忘记将 PictureBox 添加到表单中。如果没有表格存放在您的 PictureBox 中,则不会出现。您的代码可能需要将表单传递给 drawCharacter() 方法,并在方法内部添加如下内容:

YourFormVariable.Controls.Add(playberBox)

附带说明:您不想一遍又一遍地向表单添加 PictureBoxes,您想要添加一个并不断地操作它。我只是将此作为警告提及,因为 drawCharacter() 听起来它可以在 "render" 类型循环中调用。

问题是您没有将表单实例传递给 class,因此它无法对其创建控制。将您的 drawCharacter 方法更改为:

  class MainCharacter
    {
    string characterName;
    int characterGender;
    bool registeredUser;
    int[] playerPosition;

    // Character constructor
    public MainCharacter(string name, int gender, bool registered, int[] location)
    {
        characterName = name;
        characterGender = gender;
        registeredUser = registered;
        playerPosition = location;
    }

    public void drawCharacter(Form form)
    {
        PictureBox playerBox = new PictureBox();
        playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx;
        playerBox.Width = 28;
        playerBox.Height = 32;
        playerBox.Location = new Point(playerPosition[0], playerPosition[1]);
        playerBox.Visible = true;
        form.Controls.Add(playerBox);
    }
}

并像这样使用它:

MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);

player1.drawCharacter(this);