【问题标题】:Editing properties of a PictureBox from another class从另一个类编辑 PictureBox 的属性
【发布时间】:2014-03-14 15:14:15
【问题描述】:

上下文:我正在尝试在 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_Click 的最后一行时,似乎什么也没发生(图像没有改变)。

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

【问题讨论】:

  • getPictureBox() 获得了什么图片框?是否等于null
  • 在这个例子中(我正在搜索的检查器数组中唯一的检查器是checker1)所以它会得到PictureBoxchecker1相关联编辑 - 不,@987654328 @ 不返回 null。
  • topMost.getPictureBox().BackgroundImage = null; 这段代码是否清空了图片框?还有一件事,你用的是变量i,它在哪里声明的?
  • 您可能需要致电topmost.getPictureBox().Refresh()
  • @DonBoitnott 或topmost.getPictureBox().Invalidate();

标签: c# winforms picturebox


【解决方案1】:

既然棋子与图片框相连,为什么不在 Checker 类中为它们创建一个新的引用属性呢?

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

// 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;
        }
    }
}

将图片框属性添加到您的 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();

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多