简体   繁体   中英

change form background image through a button click

I want to change the form's back ground image when I click the button. I'm stuck at this error. It says:

An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.BackgroundImage.get'

    private void pictureBox1_MouseHover(object sender, EventArgs e)
    {
        pictureBox1.Location = new Point(25, 9);
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        pictureBox1.Location = new Point(18, 9);
    }


    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {

        Form1.BackgroundImage = 
    }

On the last part of the code, you can see that I am attempting to change the background image of the form. but it does not allow me and I don't know how to do it properly.

Use this instead of Form1 :

this.BackgroundImage = ...

Form1 is a Type , not an Instance of an Object, you're looking for this .

if you want to use Form1 use this:

Form1 f1 = new Form1();
f1.BackgroundImage = ...

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