如何让类在窗体中创建PictureBox对象

时间:2015-10-04 15:21:24

标签: c# winforms picturebox

我正在尝试创建一个MainCharacter类,其作用是根据每次加载时通过房间传递的一些参数在“房间”对象内创建PictureBox

以下是MainCharacter类的代码:

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可见。

-----新行动-----

根据您的指示,使用Room类

中的以下代码
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类中的以下代码:

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方法中定义了图片框大小。

3 个答案:

答案 0 :(得分:2)

<强>选项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
您可以更改方法以返回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表单集合中。
  • 不要忘记将location属性设置为合适的位置或更好的选项,使用FlowLayoutPanel并将PictureBox添加到其中。例如,如果你从(flowLayoutPanel1)打开FlowLayoutPabel,你可以使用选项2并以这种方式添加picturebox

使用FlowLayoutPanel的代码:

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

答案 1 :(得分:0)

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

YourFormVariable.Controls.Add(playberBox)

作为旁注:您不希望一遍又一遍地向表单添加PictureBox es,您想要添加一个并不断地操作它。我只是提到这是一个警告,因为drawCharacter()听起来像是可以在“渲染”类型循环中调用。

答案 2 :(得分:0)

问题是您没有将表单实例传递给您的类,因此无法对其进行控制。将您的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);