erikths erikths - 2 months ago 11
C# Question

I cannot change the image in pictureBox1 from code in C#

First things first: Im pretty new to Programming, and I'm trying to learn the C# Language

My Goal: Having a method that changes the picture in pictureBox1.

Issue: I get the error that tells me that an object reference is required for non-static field.

Here's a snippet of my class where the method should go.

private class Execute
{
private void valueChecker(char value)
{
for (int i = 0; i <= charLenght; i++)
{
if (value != CharArray[i])
{
i++;
}

else if (value == CharArray[i])
{
CorrectLetter(value);
svalue = true;
}
}
if (svalue == true)
{
/* This is where the command is happening.
But I get error message : "An object reference is required for the non-static field, method or property."
*/

pictureBox1.Image = photos[x];
x++;
}
}

}


I have also tried making new classes and methods other places in the code, and call it from the if statement, but I don't get this to work.

I need to change the picture in pictureBox1 if the svalue == true

A little further info on what exactly Im doing:
Im making a hangman game as an exercise, and I want to update the Image in pictureBox1 if the input letter can't be found in the answer.

The pictures are stored in an array I have called photos[].

Answer

Your method declaration should be something like this:

public void valueChecker(char value,PictureBox pictureBox1)
Comments