简体   繁体   English

我无法从 C# 中的代码更改 pictureBox1 中的图像

[英]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第一件事:我对编程还很陌生,我正在努力学习 C# 语言

My Goal: Having a method that changes the picture in pictureBox1.我的目标:有一个方法可以改变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.我还尝试在代码的其他地方创建新的类和方法,并从 if 语句中调用它,但我没有让它起作用。

I need to change the picture in pictureBox1 if the svalue == true如果 svalue == true,我需要更改 pictureBox1 中的图片

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.关于我到底在做什么的更多信息:我正在制作一个刽子手游戏作为练习,如果在答案中找不到输入字母,我想更新pictureBox1中的图像。

The pictures are stored in an array I have called photos[].图片存储在我称为 photos[] 的数组中。

你的方法声明应该是这样的:

public void valueChecker(char value,PictureBox pictureBox1)
 var MyImage = new Bitmap(photos[x]);
 pictureBox1.Image = (Image) MyImage ;

Your class needs a reference to the PictureBox .您的课程需要对PictureBox引用 You can add a property and set it after creating an instance of the class or even pass it right into the constructor..您可以在创建类的实例后添加属性并设置它,甚至可以将其直接传递给构造函数。

private class Execute
{
   public PictureBox pBox {get; set;}

   public Execute(PictureBox  pb)
   {
        pBox  = pb;
   }

   private void valueChecker(char value)  // or maybe public ?!
   {
      ...
      ...

      if (pBox != null) pBox.Image = photos[x];
      x++;
    }
}

Create the class instance like this:像这样创建类实例:

Execute someName = new Execute(pictureBox1);

Note that there are other ways to solve this;请注意,还有其他方法可以解决此问题; this is just a rather direct and straightforward one.这只是一个相当直接和直接的。 If your class is a kind of service class you may want to go with Tarek's even more direct solution.如果您的课程是一种服务课程,您可能想要使用 Tarek 更直接的解决方案。 Note that he not just adds the PictureBox to the parameter list.请注意,他不只是将PictureBox添加到参数列表中。 He also makes the function public .他还public了该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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