Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

c++builder - BCB : how to iterate over controls on a form?

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 typeid(myForm->Controls[i]) but this gave me a few problems.

1) typeid(myForm->Controls[i])->Name always gives "TControl *" and I was hoping for "TEdit *", "TMemo *", etc

Can I get round that by using

if (typeid(myForm->Controls[i]) == typeid(TEdit))

and then casting? (if so, how best to cast?)

2) how can I (probably by casting) get the properties of the control? e.g, Name, Width, Height, etc?

I woudl really appreciate actual code here (or a URL of some actual code); thanks.


Update: Since I only need to test 5 ot 6 different types of controls for my specific case, I thought I could maybe try to dynamic_cast<> each to each of them in turn, but I can't seem to get that to work ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...