分组控件

发布于 2024-08-16 10:27:58 字数 311 浏览 7 评论 0原文

我正在使用 C++ Builder 5。有没有一种方法可以对一组不同的控件进行分组,以便通过简单地调用,例如,myGroup.Enabled = false; 将设置所有控件组enabled 属性设置为 false?我无法使用 GroupBox,因为控件(标签、复选框等)位于不同的 TabPages 上。

我问的原因是我不必显式调用每个控件的 enabled 属性,并且可以通过一个简单的调用来完成此操作。

如果没有,我如何创建自定义 Control 类来执行此操作?

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.

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.

If not, how can I create a custom Control class to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2024-08-23 10:27:58

由于要分组的控件不在同一个容器中,那么我建议使用 TAction(查看 TActionList 组件)。所有 TControl 后代都有一个公共(有时甚至已发布)Action 属性。您可以将同一个 TAction 对象同时分配给多个控件。启用/禁用 TAction(或更新其任何其他属性)将自动相应地更新所有关联的控件。

Since the controls you want to group are not in the same container, then I suggest using a TAction (look at the TActionList component). All TControl descendants have a public (sometimes even published) Action property. You can have the same TAction object assigned to multiple controls at the same time. Enabling/disabling the TAction (or updating any of its other properties) will automatically update all associated controls accordingly.

梅窗月明清似水 2024-08-23 10:27:58

您可以使用 标签属性< /a> 控件并创建您自己的分组。

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
}

或者,如果您想一次性启用/禁用所有控件。

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 进行测试

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
} 

Examples:

// disable all controls on the form
SetControlState(Form1, false);

// disable all controls on a tabsheet
SetControlState(TabSheet1, false);

NOTE: The above code has been tested with C++Builder 2007

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文