I need to get the number after the button to increment in a for loop. For example, button1 becomes button2, etc. I have tried appending a variable which increments but C++ Builder gives an error saying “Button is not a member of TMain.” Is there any way to achieve the end goal or get around this?
Share
You can’t construct new identifiers from others at run time. The compiler is correct that
Buttonreally isn’t a member of yourTMainclass.Instead, build the string name of the component you want, and then call your form’s
FindComponentmethod to get the component with that name.That requires that the buttons’
Nameproperties be set accordingly.Another solution is to forego the component names and put your objects in a proper container, like a
vector. For example, you can override theLoadedmethod (which is where you can be sure all your form’s components have been created) and fill a vector there:Now when you want to iterate over your buttons, you just iterate over the vector instead: