BCB : how to iterate over controls on a form?

后端 未结 3 1038
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 07:23

I am looking for some BCB code to iterate over the controls on a form and get some information about them.

I tried using myForm->ControlCount and

相关标签:
3条回答
  • 2020-12-17 07:37

    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.

    0 讨论(0)
  • 2020-12-17 07:51

    This may help you too:

    for (int index = 0; index < ControlCount; index ++)
    {
        if(Controls[index]->InheritsFrom(__classid(TCustomEdit)))
        {
            TCustomEdit *edit = (TEdit*) Controls[index];
            edit->Text = "Ok";
        }
    }
    
    0 讨论(0)
  • 2020-12-17 07:55

    Here it is:

    void EnumerateAll(TComponent *container)
    {
       // Enumarate its children
       for (int i = 0; i < container->ComponentCount; i++)
          {
          // extract the control at index i
          TComponent *child = container->Components[i];
    
          if ( child->InheritsFrom (__classid(TComponent)) )    //this check is optional
             Form3->Memo->Lines->Add(child->Name);
          }
    }
    

    Usage:

    EnumerateAll(MyForm);
    

    The function above will enumerate also the menu items.
    Also see: C++ builder - enumerate components on TPanel

    Here are more details about the VCL hierarchy: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Class_library

    0 讨论(0)
提交回复
热议问题