分组控件 [英] Grouping Controls
问题描述
我正在使用C ++ Builder5。是否可以对一组完全不同的控件进行分组,以便通过简单地调用 myGroup.Enabled = false;
将所有已启用
的控件组设置为false?我不能使用GroupBox,因为控件(标签,复选框等)位于不同的 TabPages
上。
I'm using C++ Builder 5. Is there a way to group a disparate set of controls so that by simply calling, for instance, myGroup.Enabled = false;
will set all group of controls enabled
property to false? I can't use a GroupBox since the controls (labels, checkboxes, etc) are on different TabPages
.
我问的原因是这样,我不必显式调用每个控件的 enabled
属性,并且可以通过一个简单的调用来实现。
The reason I ask is so I don't have to call each control's enabled
property explicitly and can do it with one simple call.
如果没有,我如何创建一个自定义Control类来做到这一点?
If not, how can I create a custom Control class to do this?
推荐答案
您可以使用< a href = http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/_!!MEMBERTYPE_Properties_Classes_TComponent_14_Published_Properties.html rel = nofollow noreferrer>标记属性控制并创建您自己的分组。
You could use the Tag property of the controls and create your own grouping.
void TForm1::SetControlState(TWinControl *WinCtrl, const bool IsEnabled, const int TagValue)
{
// set the enabled property for each control with matching TagValue
for (int Index = 0; Index < WinCtrl->ControlCount; ++Index)
{
if (WinCtrl->Controls[Index]->Tag == TagValue)
{
WinCtrl->Controls[Index]->Enabled = IsEnabled;
}
// set child controls
if (WinCtrl->Controls[Index]->InheritsFrom(__classid(TWinControl)))
{
TWinControl *TempWinCtrl;
TempWinCtrl = static_cast<TWinControl *>(WinCtrl->Controls[Index]);
SetControlState(TempWinCtrl, IsEnabled, TagValue);
}
} // end for
}
如果要一次性启用/禁用所有控件。
Alternatively, If you want to enable/disable all controls in one go.
void TForm1::SetControlState(TWinControl *WinCtrl, const bool IsEnabled)
{
// set the enabled property for each control with parent TabSheet
for (int Index = 0; Index < WinCtrl->ControlCount; ++Index)
{
WinCtrl->Controls[Index]->Enabled = IsEnabled;
// disable child controls
if (WinCtrl->Controls[Index]->InheritsFrom(__classid(TWinControl)))
{
TWinControl *TempWinCtrl;
TempWinCtrl = static_cast<TWinControl *>(WinCtrl->Controls[Index]);
SetControlState(TempWinCtrl, IsEnabled);
}
} // end for
}
示例:
// disable all controls on the form
SetControlState(Form1, false);
// disable all controls on a tabsheet
SetControlState(TabSheet1, false);
注意:上面的代码已经过C ++ Builder 2007的测试
NOTE: The above code has been tested with C++Builder 2007
这篇关于分组控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!