0
votes

I have a class which is meant to create a PictureBox on a Panel in the form 'Form1'. For example, somewhere in my code, an instance of the class 'Object' is created:

Object Object = new Object(1, "", "", false, "");

By creating this object, a PictureBox should be created in the panel 'panel1' in the form 'Form1'. I should be able to instantiate multiple Objects so (to my understanding) it cannot be static. Form1 is (to my understanding) static, and I want to be able to add multiple pictureboxes to the panel.

Object Class:

class Object
{

    private PictureBox Imageof = new PictureBox();

    //Parameters
    protected int Id { get; set; }
    protected string Name { get; set; }
    protected string Image { get; set; }
    protected bool Collision { get; set; }
    protected string Location { get; }

    //Constructor
    public Object(
        int Id, string Name, string Image, bool Collision, string Location)
    {
        this.Id = Id;
        this.Name = Name;
        this.Image = Image;
        this.Collision = Collision;
        this.Location = Location;

        Imageof.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + Image + ".png");
        Form1.panel1.Controls.Add(Imageof);
    }

}

Form1:

public partial class Form1 : Form
{

public Form1()
{
    InitializeComponent();
    LoadAll LoadFiles = new LoadAll();
    LoadFiles.Load();
}

private void Form1_Load(object sender, EventArgs e)
{

}

public void panel1_Paint(object sender, PaintEventArgs e)
{

}

However I keep getting an error:

An object reference is required for the non-static field, method, or property 'Form1.panel1'

I've searched everywhere for this error. I'm quite new to C# and I can't for the life of me understand exactly how to fix it.

1
To explain this to you, Entire Object Oriented Programming concept need to be invoked, in a nutshell, there is a blueprint, you call types, which in your case, Form1 is such a type. Whenever you need one such thing, you create an instance and you give it a name and you store them in a variable. You need access to one such variable which as such a type stored in to make changes to its internals, in your case, its children. Here you are trying to change its children through the blueprint rather than the instance. Blueprint is used for only accessing static members, to say the least. - Mat J
If I created an instance of Form1, how would I display the form if it is created upon starting the program? - Konrad Czajkowski
You can pass the instance of Form1 on the constructor of the Object class. But I believe your object class could be a method or an extension method of Form1 class. - Lutti Coelho
So would I have to create an instance of Form1 somewhere in the program, and then reference everything to that instance? - Konrad Czajkowski
Look at the Program.cs file, there is an instance created which is what get displayed on the screen, You need to find a way to grab that instance. To help with that we need more information which you conveniently glazed over with "somewhere in my code". That somewhere will determine how you grab the instance. - Mat J

1 Answers

0
votes

You got this error because you try to use a non static class withou create a new instance of that. As your Form1 is not static you should instantiate it before call any property or method.

Other approach is pass an instance of Form1 on the class constructor.

class Object
{

    private PictureBox Imageof = new PictureBox();
    private Form1 _form1;

    //Parameters
    protected int Id { get; set; }
    protected string Name { get; set; }
    protected string Image { get; set; }
    protected bool Collision { get; set; }
    protected string Location { get; }

    //Constructor
    public Object(Form1 form1,
        int Id, string Name, string Image, bool Collision, string Location)
    {
        this.Id = Id;
        this.Name = Name;
        this.Image = Image;
        this.Collision = Collision;
        this.Location = Location;

        Imageof.Image =     System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + Image + ".png");

        _form1 = form1;
        _form1.panel1.Controls.Add(Imageof);
    }

}