简体   繁体   English

我怎样才能得到被点击的图片框的数量?

[英]How can i get number of PictureBox that was clicked?

I make Tic Tac Toe game, i have 9 cells that the player can click.我制作 Tic Tac Toe 游戏,我有 9 个玩家可以点击的单元格。 Initially cell don't have any picture, but when cell was clicked it's picture must changes.最初单元格没有任何图片,但是当单击单元格时,它的图片必须更改。 How can i create this event for every cell without code duplicate?如何在没有代码重复的情况下为每个单元格创建此事件?

The signature for a click event has a "sender" object.单击事件的签名有一个“发送者”对象。 You can use that for reference.您可以将其用作参考。 Cast it to the appropriate type, and you can access all its public properties directly.将其转换为适当的类型,您可以直接访问其所有公共属性。

You should be able to do something like:您应该能够执行以下操作:

protected void pictureClick(Object sender, EventArgs e) {
    PictureBox pic = sender as PictureBox;

    if (pic != null) {
        // set the image based on which players turn it is.
    } 
}

and now each picture box would have its onClick event set to this one pictureClick function.现在每个图片框都将其 onClick 事件设置为这个一个 pictureClick 函数。

--- Edit --- - - 编辑 - -

I'd also add that using independent UI controls in this manner is extremely inefficient for large tile games.我还要补充一点,以这种方式使用独立的 UI 控件对于大型瓷砖游戏来说效率极低。 For a 3x3 tic-tac-toe grid, it's probably fine, but for something like a 8x8 chessboard, the refresh times for all 64 squares is going to be noticeable, because every UI component on the page has its Paint() method called when the window refreshes.对于 3x3 tic-tac-toe 网格,它可能没问题,但对于 8x8 棋盘之类的东西,所有 64 个方格的刷新时间将是明显的,因为页面上的每个 UI 组件都有其 Paint() 方法调用时窗口刷新。 I'm speaking from experience here.我是从这里的经验说起。 I once tried to use this approach of having a grid of 10x10 Panel components with custom draw methods based on the game state data, and anytime the window was resized, the game would hang for several seconds while everything would refresh.我曾经尝试使用这种方法来使用基于游戏状态数据的具有自定义绘制方法的 10x10 面板组件的网格,并且无论何时调整窗口大小,游戏都会挂起几秒钟,而一切都会刷新。

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

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