How to access form elements from another class

12,147

[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

Share:
12,147
asdfkjasdfjk
Author by

asdfkjasdfjk

Updated on June 04, 2022

Comments

  • asdfkjasdfjk
    asdfkjasdfjk almost 2 years

    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?