Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access form elements from another class

Tags:

c#

oop

winforms

I have following code

   namespace Spaceship_Invaders
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Image myImage = Image.FromFile("image/Untitled.png");
                pictureBox1.Image = myImage;
            }

            public class spaceship { 
                Image myimage = Image.FromFile("image/Untitled6.png");
                Form1 myform = new Form1();
                 myform.pictureBox1.Image = myimage;            


            }

        }
    }

I have a picturebox in the form and I want to access the picturebox from the class spaceship but i can not access it. How to do this?

like image 447
asdfkjasdfjk Avatar asked Dec 18 '25 20:12

asdfkjasdfjk


1 Answers

[EDITED] You can access it this way:

public class spaceship
{ 
    Image myimage = Image.FromFile("image/Untitled6.png");
    Form1 myform = new Form1();

    spaceship()
    {
        myform.pictureBox1.Image = myimage;             
    }
}

Have a look here

like image 112
Hossein Narimani Rad Avatar answered Dec 21 '25 12:12

Hossein Narimani Rad