因為對.net了解不是太深入,所以只能做出這樣的水平:
找到要查找的反射屬性信息:
PropertyInfo^ getPropertyInfo(Type^ t, String^ pName) {
PropertyInfo^ pInfo;
while (t != nullptr) {
pInfo = t->GetProperty(pName, BindingFlags::DeclaredOnly | BindingFlags::Public | BindingFlags::Instance);
if (pInfo != nullptr)
{
return pInfo;
}
t = t->BaseType;
}
return nullptr;
}
從一個Component開始查找,然後查找其子Component是否有名為compName的控件,有則返回,無則返回nullptr
// get a component by it's name, the component is in comp
Component^ getComponentByName(String^ compName, Component^ comp) {
if (nullptr == comp)
{
return comp;
}
// if this component is the right one, then return it
Type^ t = comp->GetType();
PropertyInfo^ pInfo = t->GetProperty("Name");
if (pInfo != nullptr && compName->Equals(dynamic_cast<String^>(pInfo->GetValue(comp, nullptr))))
{
return comp;
}
// search this component's children Controls
Component^ retComp;
pInfo = getPropertyInfo(t, "Controls");
if (pInfo != nullptr)
{
System::Collections::IList^ list = safe_cast<System::Collections::IList^>(pInfo->GetValue(comp, nullptr));
if (list != nullptr)
{
for (int i = 0; i < list->Count; i++)
{
if (nullptr != (retComp = getComponentByName(compName, safe_cast<Component^>(list[i]))))
{
return retComp;
}
}
}
}
// search this component's children Items
pInfo = getPropertyInfo(t, "Items");
if (pInfo != nullptr)
{
System::Collections::IList^ list = safe_cast<System::Collections::IList^>(pInfo->GetValue(comp, nullptr));
if (list != nullptr)
{
for (int i = 0; i < list->Count; i++)
{
if (nullptr != (retComp = getComponentByName(compName, safe_cast<Component^>(list[i]))))
{
return retComp;
}
}
}
}
return nullptr;
}
在屬性面板上面一點好像有個下拉文本框,可以在那裡選擇的把!
老實說,有更好的辦法實現你的功能...
1.使用控件數組.
2.使用集合對象.
這兩種方法都可以實現你的功能.
控件數組很方便啊.就跟數組似的.
集合就更好用啦.往裡面添加TextBox類型的控件作為元素.