I'm new to using C++ Builder so apologies if I'm making any rudimentary mistakes.
I have drawn out a TLayout named 'Collection' with a 5x5 grid of TRectangles within it. The cells are named like so "CellXY". I presumed it might be easier to draw these out on the Form rather than instantiating them with code, now I'm thinking otherwise, but I would still like to solve the problem this way to better my understanding.
I'm trying to write a method which will return a pointer to a TRectangle whose name contains the coordinates passed to the method.
Currently, I'm trying to do this by iterating through the children of the TLayout Collection:
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;
}
But I am given an error reading:
E2031 Cannot cast from 'TFmxChildrenList' to 'TRectangle *'
If anyone could help, I'd be very grateful!
The Children
property is a pointer to a class type (TFmxChildrenList
) that internally holds an array of objects. Children
is not the actual array itself, like you are trying to treat it as.
Children[cIndex]
is using pointer arithmetic, which is not what you want in this situation. You need to use the Children->Items[]
sub-property instead, by changing this statement:
Collection->Children[cIndex]
To either this:
Collection->Children->Items[cIndex]
Or this:
(*(Collection->Children))[cIndex]