c# - 我怎样才能让一个类在表单内创建一个 PictureBox 对象

标签 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

-----新 Action -----

按照您的指示,在 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方法中定义了图片框的大小,但我设法在表单内绘制了一些东西,但它看起来像一条非常小的线。

最佳答案

选项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 集合中。
  • 不要忘记将位置属性设置为合适的位置或更好的选项,使用 FlowLayoutPanel 并将 PictureBox 添加到其中。例如,如果您在 (flowLayoutPanel1) 上放置 FlowLayoutPabel,则可以使用选项 2 并以这种方式向其添加图片框

使用 FlowLayoutPanel 的代码:

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

关于c# - 我怎样才能让一个类在表单内创建一个 PictureBox 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32935071/

相关文章:

c# - IObservable REST 客户端

c# - 使用 Json.net 反序列化 JSON 对象数组

c# - 如何在特定行和列添加复选框?

C# 从一个图片框拖放到另一个图片框

C# 俄罗斯方 block 游戏性能缓慢

c# - 如何在不立即读取整个文件的情况下编辑文本文件

c# - 按 Messagebox 上的帮助按钮时事件未触发

c# - 如何绕过 C# (Winforms) 中缺少布局和 JPanel

c# - 将 PictureBox 中的图像调整到尽可能大,同时保持纵横比?

c# - 有序并行未按预期工作。 (将 List 转换为 IEnumerable?)