You are somewhat correct in your assumption that casting would be a good idea and that using dynamic_cast
is the best option here.
If you want to iterate over the controls of a form (or any other container). Unfortunately I don't have my C++ Builder on this computer so I am unable to test the code I give you, should be a trivial task to create though.
// You wanna start my iterating through the controls of the container
for (int i = 0; i < container->ControlCount; i++)
{
// Let us extract the control at index i
TControl *control = container->Controls[i];
// Now we want to test whether this is any specific type, for instance a
// TEdit or any TEdit descendant.
TEdit *edit = dynamic_cast<TEdit *>(control);
// If this control is a TEdit or any control deriving from it, we will have
// a pointer to it, if this is any other type, we will be left with a zero
// pointer.
if (edit)
{
// This is a TEdit control... we can now access TEdit only properties.
edit->Text = "This is an edit control.";
}
// We do the same if we want to test for a TButton...
TButton *button = dynamic_cast<TButton *>(control);
// Again we test whether this was actually a button
if (button)
{
// It is a button, again we can access TButton only properties...
button->Caption = "This is a button";
// Yeah ok not a TButton only one, but couldn't think of any.
}
// And so on...
}
You do not need to make the function recursive for child controls of child controls, these are included here as well by the VCL. This is in my opinion by far the simplest solution for testing for specific types. I also try to avoid the RTTI features as much as possible, but that is just me.
As you can see my example also shows how you can access properties and functions of the control, even those specific to a certain type. However the ones you mention: Name
, Width
and Height
are all common to TControl
and it should not be necessary to cast to access these.
Hope this has helped you out, it should be clear from my example that the variable container
can be replaced by myForm
which you use in yours.