我正在从以下示例中翻译以下内容 SplitContainer Splitter Gets Focus Rectangle?
private: Control getFocused(Control::ControlCollection controls)
{
//foreach (Control c in controls)
//{
// if (c.Focused)
// {
// // Return the focused control
// return c;
// }
// else if (c.ContainsFocus)
// {
// // If the focus is contained inside a control's children
// // return the child
// return getFocused(c.Controls);
// }
//}
do
{
if (c.Focused)
{
// Return the focused control
return c;
}
else if (c.ContainsFocus)
{
// If the focus is contained inside a control's children
// return the child
return getFocused(c.Controls);
}
}
while(Control c in controls);
// No control on the form has focus
return null;
}
我正在为DO WHILE循环寻找合适的synthanx
while(Control c in controls); // error
由于函数“private: Control getFocused(Control::ControlCollection controls)
”属于Control类型,我需要指定一个返回值,“return null;
”和“return nullptr;
”都会失败!
编辑:
for(int index = 0; index <= controls.Count; index++)
{
if(controls[index]->Focused)
{
return controls[index];
}
else if (controls[index]->ContainsFocus)
{
return getFocused(controls[index]->Controls);
}
}
return controls[index];
- &gt; 没有合适的用户定义的从“System :: Windows :: Forms :: Control ^”到“System :: Windows :: Forms :: Control”的转换。
return getFocused(controls[index]->Controls);
- &gt; function“getFocused”无法使用给定的参数列出参数列表参数类型是:(System :: Windows :: Forms :: Control :: ControlCollection ^)
return null;
- &gt; 标识符“null”未定义
或
return nullptr;
- &gt; 没有合适的用户定义的转换,从“decltype(nullptr)”到“System :: Windows :: Forms :: Control”存在
答案 0 :(得分:1)
我的C ++ / CLI有点生疏。但是,让我这样说吧:
private: Control ^ getFocused(Control::ControlCollection ^controls)
{
for each (Control ^c in controls)
{
if (c->Focused)
{
return c;
}
else if (c->ContainsFocus)
{
return getFocused(c->Controls);
}
}
return nullptr;
}
答案 1 :(得分:0)
MD.Unicorn在技术上是正确的,因为没有“foreach”的C ++语法。但是,C ++确实包含了STL中非常相似的东西(想想.net框架而不是C#本身)。
for_each(foo.begin(), foo.end(), [=](Control control){
//Stuff
});