简体   繁体   中英

trying to change a picture box c#

Im trying to make my picture change when a button is clicked so that the player can see the character attack. When they do so, so the picture should change to the attack image when the button is clicked and then back to the old image when the opponents turn starts.

All the images are saved in a folder named "images" inside the project folder, which is inside the "WindowsFormsApplication1" folder, but it says it cant find namespace images inside windowsformsapplication1

Here is the code I am using to change the image:

private void ArBut_Click(object sender, EventArgs e)
        {
            if (playerturn == true)
            {
                Ar.Image = global::WindowsFormsApplication1.images.archeratack.jpeg;
                drhp = drhp - 15;
                DrHP.Text = drhp.ToString();
                checkend();
                playerturn = false;
                dratak();
            }
        }

You can't just set the image like that. First of all, because thats not a String , it thinks you are trying to access a code element. Obviously images is not a namespace or class within your code, so you get the compiler error.

You need to use PictureBox.Load ( MSDN )

Ar.Load(@"./images/archeratack.jpeg");

您可以尝试创建一个Image对象并使用Image.FromFile(“ url”)

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