无法从“ TFmxChildrenList”转换为“ TRectangle *”

时间:2018-08-07 11:51:54

标签: c++builder c++builder-10.2-tokyo

我不熟悉C ++ Builder,因此如果犯任何基本错误,我深表歉意。

我绘制了一个名为“ Collection”的TLayout,其中包含5x5的TRectangles网格。像这样命名单元格“ CellXY”。 我以为将它们绘制在Form上比用代码实例化它们更容易,现在我在想别的办法,但是我还是想以此方式解决问题,以加深我的理解。

我正在尝试编写一种方法,该方法将返回指向TRectangle的指针,该名称的名称包含传递给该方法的坐标。

当前,我正在尝试通过遍历TLayout集合的子代来实现此目的:

TRectangle* __fastcall TForm2::getCellRectangleFromCoordinate(int X, int Y){
    TRectangle* targetCell = NULL;
    char targetCellName[6];
    sprintf(targetCellName, "Cell%i%i", X, Y);
    for (int cIndex = 0; cIndex < Collection->ChildrenCount; ++cIndex)
    {
        TRectangle* cellRect = (TRectangle*) Collection->Children[cIndex]; // Error Here
        string cellName = cellRect->Name;
        if (targetCellName == cellName) {
            targetCell = cellRect;
            break;
        }
    }
    return targetCell;
}

但是阅读时出现错误:

E2031 Cannot cast from 'TFmxChildrenList' to 'TRectangle *'

如果有人可以帮助我,我将非常感激!

1 个答案:

答案 0 :(得分:0)

Children属性是指向内部保存对象数组的类类型(TFmxChildrenList)的指针。 Children并不是实际的数组本身,就像您尝试将其视为一样。

Children[cIndex]使用的是指针算法,在这种情况下这不是您想要的。您需要通过更改以下语句来使用Children->Items[]子属性:

Collection->Children[cIndex]

为此:

Collection->Children->Items[cIndex]

或者这个:

(*(Collection->Children))[cIndex]