Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个类编辑PictureBox的属性_C#_Winforms_Picturebox - Fatal编程技术网

C# 从另一个类编辑PictureBox的属性

C# 从另一个类编辑PictureBox的属性,c#,winforms,picturebox,C#,Winforms,Picturebox,上下文:我正在尝试在Windows.Forms中制作一个双陆棋的小游戏。我有3个类GameBoard.cs、Points.cs和Checkers.cs(以及Form1.cs)。我需要能够做的是使用我的一些其他类更改PictureBox的某些属性的值 具体来说,我有以下代码: // gameBoard.cs namespace backgammon { public class gameBoard { Checker checker1; Poin

上下文:我正在尝试在Windows.Forms中制作一个双陆棋的小游戏。我有3个类GameBoard.cs、Points.cs和Checkers.cs(以及Form1.cs)。我需要能够做的是使用我的一些其他类更改PictureBox的某些属性的值

具体来说,我有以下代码:

// gameBoard.cs

namespace backgammon
{
    public class gameBoard
    {

        Checker checker1;
        Points point1;
        Points[] pointsArray;

        public gameBoard()
        {
             // make new checker (ID, PictureBox, startingPoint)
             checker1 = new Checker(1, checkerPicBox1, 1);

             // make new Point (ID, arrayOfCheckers)
             point13 = new Points(1, new Checker[]{checker1 /*,checker2... etc*/});

             pointsArray = new Points[MAX_POINTS];
             pointsArray[0] = point1;
        }
    }
}
这就是我如何“设置”跳棋和点数的方法。我的checker和point类可以获取并设置传递到其构造中的所有变量

问题:我试图实现的是在点击检查器后“突出显示”它

在表格1中:

// Form1.cs

private void checkerPicBox1_Click(object sender, EventArgs e)
{
    int pointNumber = gameBoard.checker1.getPointMember();

    // find the top most checker in the checker array so we can highlight it 
    Checker topMost = gameBoard.pointsArray[pointNumber - 1].getCheckerFromIndex(gameBoard.pointsArray[pointNumber - 1].getCheckerArray().Length - 1);

    // get the picturebox and change the image
    topMost.getPictureBox().BackgroundImage = global::Backgammon.Properties.Resources.blackCheckerSelected;
}
代码编译并运行,但当它到达checkerPicBox\u的最后一行时,似乎什么都没有发生(图像没有改变)


这里发生了什么?我没有正确的PictureBox实例吗?或者我是以一种奇怪的方式/不是我应该的方式来做的?

既然Checker连接到picturebox,为什么不在Checker类中为它们创建一个新的引用属性呢

您可以在构造函数中传递包含picturebox的表单,并使用该表单创建对正确picturebox实例的引用

// gameBoard.cs

namespace backgammon
{
    public class gameBoard
    {
        Checker checker1;
        Points point1;
        Points[] pointsArray;

        public gameBoard(Form gameForm)
        {
             // make new checker (ID, PictureBox, startingPoint)
             checker1 = new Checker(1, gameForm.checkerPicBox1, 1);

             // make new Point (ID, arrayOfCheckers)
             point13 = new Points(1, new Checker[]{checker1 /*,checker2... etc*/});

             pointsArray = new Points[MAX_POINTS];
             pointsArray[0] = point1;
        }
    }
}
将picturebox属性添加到Checker类:

// checker.cs

public class Checker
{
   PictureBox _picturebox;

   //... other code here

   public Checker(int ID, PictureBox picturebox, Points startingPoint)
   {
      _picturebox = picturebox;

      //...other code here
   }
}
然后我们可以在游戏形式中使用:

gameBoard gameBoard1 = new gameBoard(this);
gameBoard1.checker1._picturebox.BackgroundImage = global::Backgammon.Properties.Resources.blackCheckerSelected;
gameBoard1.checker1._picturebox.Invalidate();

使用
getPictureBox()
,您得到了什么样的pictureBox?它是否等于
null
?在本例中(我正在搜索的检查器数组中唯一的检查器是
checker1
),因此它将获得与
checker1
编辑关联的
PictureBox
,否,
getPictureBox()
不会返回null。
最上面的.getPictureBox()。BackgroundImage=null此代码是否清空pictureBox?另一件事,您正在使用变量
i
,它在哪里声明?您可能必须调用
topmost.getPictureBox().Refresh()
@DonBoitnott或
topmost.getPictureBox().Invalidate()