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.
types, which in your case,Form1is such a type. Whenever you need one such thing, you create aninstanceand 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