简体   繁体   English

当我不控制子对象时,从子对象中识别出父对象

[英]Identifying a parent object from a child object, when I don't control the child object

Is there a standard pattern for identifying the parent object of a child object in a composite object, when you can't set data in the child object at creation? 当您无法在创建时在子对象中设置数据时,是否存在用于标识复合对象中子对象的父对象的标准模式?

I'm making a chess-like board. 我正在制作象棋一样的棋盘。 The board is composed of a grid of picture boxes. 该板由图片框网格组成。 To model the board I keep an array of "uxSquares", which contain the picture box plus some other data I'll need. 为了给电路板建模,我保留了一个“ uxSquares”数组,其中包含图片框以及一些我需要的其他数据。 Something akin to: 类似于:

private class uxSquare
{
    public int RowIndex { get; private set; }
    public int ColumnIndex { get; private set; }

    // ... other stuff

    public uxSquare(int RowIndex, int ColumnIndex)
    {
        this.RowIndex = RowIndex;
        this.ColumnIndex = ColumnIndex;
        this.PictureBox = new PictureBox();
        this.PictureBox.Name = "R" + RowIndex.ToString() +
            "C" + ColumnIndex.ToString();
        }
    }

During form load I create a 2 dimensional (row, col) array of uxSquares, and place the picture box from each uxSquare on the form in a grid layout. 在加载表单的过程中,我创建了一个uxSquares的二维(行,列)数组,并将每个uxSquare的图片框放置在网格上的表单上。 With the creation of each Picture Box, I'm storing the uxSquares[,] indices in the name of the PictureBox (seen in the uxSquare constructor above). 创建每个图片框后,我将uxSquares [,]索引存储在PictureBox的名称中(在上面的uxSquare构造函数中看到)。

for (int rowIndex = 0; rowIndex < countOfRows; rowIndex++)
{
    for (int colIndex = 0; colIndex < countOfColumns; colIndex++)
    {
        // create the uxSquare array
        uxSquares[rowIndex, colIndex] = new uxSquare(rowIndex, colIndex);

        // ... place pictureBox in gridlayout, plus more stuff
    }
}

When the user clicks on a Picture Box, I need to know which uxSquare in the array owns that picture box control. 当用户单击图片框时,我需要知道数组中的哪个uxSquare拥有该图片框控件。 Currently, in the shared click handler for the Picture Boxes, I'm parsing the name of the Sender object to get the uxSquares indices. 当前,在“图片框”的共享单击处理程序中,我正在解析Sender对象的名称以获取uxSquares索引。

This seems like a gross hack, keeping array indices in the name of the control. 这似乎是一个骇人听闻的破解,将数组索引保留在控件的名称下。 However, my alternative appears to be keeping a separate lookup table mapping picture box control to uxSquare, which seems like an equally egregious hack. 但是,我的替代方法似乎是保留一个单独的查找表,将图片框控件映射到uxSquare,这似乎同样令人震惊。

Is there a standard pattern for identifying the parent object of a child object in a composite object, when you can't set data in the child object at creation? 当您无法在创建时在子对象中设置数据时,是否存在用于标识复合对象中子对象的父对象的标准模式? If I owned the child object, I would pass the parent object into its constructor and save it then. 如果我拥有子对象,则将父对象传递到其构造函数中,然后保存它。 However, I don't have that kind of access to the Picture Box control. 但是,我没有对Picture Box控件的访问权限。

You can subclass the picture box and pass in the row and column indices to the constructor. 您可以将图片框子类化,并将行和列索引传递给构造函数。

public class UxPicBox : PictureBox {
    public UxPicBox(int col, int row) : base() {
        this.Col = col;
        this.Row = row;
    }

    public int Col { get; set; }
    public int Row { get; set; }        

}

That way when the click event runs you can cast the sender as UxPicBox and get what you need to find the UxSquare. 这样,当click事件运行时,您可以将发送方强制转换为UxPicBox,并获取查找UxSquare所需的内容。

Edit: This is an example of the click event 编辑:这是单击事件的示例

void UxPicBox_Click(object sender, EventArgs e) {
    UxPicBox pb = (UxPicBox)sender;
    MessageBox.Show(pb.Col.ToString() + " -- " + pb.Row.ToString());
}

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

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