BCB:如何遍历表单上的控件? [英] BCB : how to iterate over controls on a form?

查看:106
本文介绍了BCB:如何遍历表单上的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些BCB代码来遍历表单上的控件并获取有关它们的信息。

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

我尝试使用 myForm -> ControlCount typeid(myForm-> Controls [i]),但这给了我一些问题。

I tried using myForm->ControlCount and typeid(myForm->Controls[i]) but this gave me a few problems.

1) typeid(myForm-> Controls [i])->名称始终给出 TControl * ,我希望获得 TEdit *, TMemo *等

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

我可以使用

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

然后进行投射? (如果是这样,如何最好地投射?)

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

2)我如何(可能通过投射)如何获得控件的属性?例如,名称,宽度,高度等?

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

我真的很喜欢这里的实际代码(或一些实际代码的URL);谢谢。

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

更新:由于我只需要针对我的特定情况测试5种ot 6种不同类型的控件,所以我认为我可以也许尝试依次对每一个分别进行 dynamic_cast<> ,但我似乎无法使其正常工作...

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 ...

推荐答案

假设转换是个好主意,并且使用 dynamic_cast 是正确的假设,那么您的说法是正确的最好的选择。

You are somewhat correct in your assumption that casting would be a good idea and that using dynamic_cast is the best option here.

如果要遍历表单(或任何其他容器)的控件。不幸的是,我在这台计算机上没有C ++ Builder,因此我无法测试我给您的代码,尽管创建起来应该是一件微不足道的任务。

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...
}

对于子控件的子控件,您无需使该函数递归,VCL也在其中包含这些子控件。在我看来,这是测试特定类型的最简单解决方案。我也尽量避免使用RTTI功能,但这就是我。

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.

您可以看到我的示例还显示了如何访问属性和函数。控件,甚至包括特定于特定类型的控件。但是,您提到的是:名称宽度高度都是 TControl 所共有的,因此不必强制进行访问。

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.

希望这对您有所帮助可以看出,从我的示例中可以清楚地看到,变量 container 可以替换为您在自己的变量中使用的 myForm

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.

这篇关于BCB:如何遍历表单上的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆