简体   繁体   中英

Access form object from another cs file

I try to display image in PictureBox and text in Label in from from another cs file i do this code but didn't work correctly any help?!

Form1 d = new Form1();
d.pictureBox1.Image = Image.FromFile(@"C:\Users\a\Desktop\tuio.jpg");
                d.label1.Text = "A" + "APPLE";
                d.Show();

when the form open doesn't respond and the program stoped.

You can't change the PictureBox or the Label from the original instance by using the above code because instead you've created a new instance and modified that one instead. What I'd do is change the PictureBox from public to public static and do the same with the Label . Then, if in any code you've referred to either the Label or the PictureBox using this. , simply remove this and then the PictureBox and Label should be accessible from any class. You would access it by using Form1.picturebox1.Image = [...] and Form1.label1.Text = [...] .

If you set the modifiers property of your picturebox and label to public and if your Picture path is correct, then calling

d.ShowDialog();

should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM