Loop with objects

Hi

I really need some help here.

I need af more dynamic way to apply changes to objects I use in a form

Say I have a form with 20 Switches (Form->Switch_name1 etc)

I my turnbased game, the switches may change from turn to turn, and sometimes you just want to reset all.

So - I would like to have a file.txt with all the Switch names (objects), that I can use to design a loop, that runs through the list of Switches and asignes the prober settings. The setting are loaded from another file / save-game file etc.

I can't use a string to asign any object... but somehow I would like to create a vector with the objects, matching the list in file.txt containing the names of all the Switches.

Could anyone help me with this and a bit of code to show an example.

Thanx

Kind regards
PAS
Say I have a form with 20 Switches (Form->Switch_name1 etc)

How do you create such form?
I use C++ Builder, and have designed different forms, with objects in (on). So the objects are not dynamically created as the game progresses, if that is, what you are thinking. The are part of a predesigned form. The names of the objects (Swiches) are just copied/written into a txt-file...

Are the switches objects "owned" by the form object? Does the form object have interface to enumerate its "children"?
Yes, correct. The form, or lets call it a window, is the owner of the other objects, say Children.

For the last part - my english may not be good enough - i am not sure, that I understand what you mean by enumerate its children. But i found this in more detail:

Owner and Parent: a window is referred to as a parent when there are, or there can be, other windows that depend on it. The Standard toolbar of C++Builder is an example of a parent window. It is equipped with buttons and acts as their parent. When a parent is created, it "gives life" to other windows that can depend on it. The VCL provides various types of parent controls. When a parent is destroyed, it also destroys its children.
An owner is a control that "owns" another control. The owner must be a parent type. An owner "carries", "holds", or hosts the controls that it owns. When an owner is created, made active, or made visible, it gives existence and visibility to its child controls. When an owner gets hidden, it also hides its children. If an owner moves, it moves with its controls. The controls keep their positions and dimensions inside the owner


Child: A window is referred to as child when its existence and its visibility depend on another window called its parent or owner. All of the Windows controls you will use in your applications are child controls and they must be parented or owned by another control

Last edited on
Qt (an another GUI framework) has method children() that returns a list of children of this QObject.
https://doc.qt.io/qt-5/qobject.html#children
One can then loop over the list, and if item is a "switch", do something.
Something like:
1
2
3
4
5
6
7
8
9
10
std::map<string,sometype> settings;
// read settings from file

for ( auto& widget : window->children() ) {
  // QObjects can have a name attribute, set in builder
  auto it = settings.find( widget->objectName() );
  if ( it != settings.end() ) {
    // update *widget with it->second
  }
}
Last edited on
If I remember correctly from my old Delphi times TForm had properties called Controls. Basically you need to loop through all the controls and check if they are switches.
This might give you some ideas:
https://stackoverflow.com/questions/2391325/bcb-how-to-iterate-over-controls-on-a-form
Last edited on
It has been several years since I used C++Builder, it was version 6, I don't remember "switch" being one of the Windows-based components that can be used on a form. Visible or not.

Win32 has check boxes and radio buttons that have switch-like operability available as standard components with Delphi and C++B.
Registered users can post here. Sign in or register to post.