Orestes Moressis
Orestes Moressis

Reputation: 35

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

I am trying to create a MainCharacter class whose job is to create a PictureBox inside a "room" object according to some of the parameters that are passed through the room every time it loads.

Here is the code of the 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;    
        }
    }
}

And the code line that creates the player object inside room1 for example

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

The problem is that no PictureBox is visible inside the form when entering the specific room that creates the player object.

----- New actions -----

As per your directions, using the following code inside the Room 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);
}

And the following code inside the 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);
}

I managed to draw something inside the form but it seems like a very small line, although I defined the picturebox size inside the drawCharacter method.

Upvotes: 2

Views: 4701

Answers (3)

Reza Aghaei
Reza Aghaei

Reputation: 125197

Option1
having an instance of your form, add this code at the end of drawCharacter:

formInstance.Controls.Add(playerBox);

For example:

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

    formInstance.Controls.Add(playerBox);
}

then in your form, when you need to call this method use:

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

Option2
You can change your method to return a PictureBox:

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

    return  playerBox;
}

then in your form, when you need to call this method use:

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

Key Point:

  • As mentioned in comments by "Ivan Stoev", all controls must be added to Controls collection of form.
  • Don't forget to set location property to a suitable location or better option, Use a FlowLayoutPanel and add your PictureBox to it. For example if you put a FlowLayoutPabel on from (flowLayoutPanel1), you can use option 2 and add picturebox to it this way

Code using FlowLayoutPanel:

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

Upvotes: 2

Fabjan
Fabjan

Reputation: 13676

Problem is that you do not pass your form instance to your class so it cannot create control on it. Change your drawCharacter method to something like:

  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);
    }
}

And use it like:

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

player1.drawCharacter(this);

Upvotes: 0

You forgot to add the PictureBox to the form. Without a form to be housed in your PictureBox won't show up. Your code may need a pass a form into the drawCharacter() method and add something like the following inside of the method:

YourFormVariable.Controls.Add(playberBox)

As a side note: you don't want to keep adding PictureBoxes to the form over and over, you want to add one and continually manipulate it. I only mention this as a warning because drawCharacter() sounds like it could be called in a "render" type loop.

Upvotes: 0

Related Questions