【发布时间】:2011-01-08 17:52:50
【问题描述】:
我在表单上有弹出菜单控件(grr,我很可能会将其设为动态 - 讨厌静态工具)。它有带有子菜单的项目。 SubMenu 有三个菜单项(TMenuItem 类)。
每当带有 SubMenu 的 Item 或 SubMenu Items 调用过程时,我需要通过在 if..then 语句中获取 Sender 参数来检查。
我用类型转换和超类操作尝试了不同的变化,但没有运气。我认为有可能是这样的:
if FindControl(MenuItemWithSubMenu.Handle) = TControl(Sender as TComponent).Parent then ...
但是,当然,正确的类型转换和命令..
任何想法表示赞赏。
社区要求的其他信息:
代码 itsef(如果我只是通过组件名称 prop 检查)如下所示:
procedure TForm1.xClick(Sender: TObject); // procedure that has attached onClick from PopupActionBar1 Items
begin
if ((TComponent(Sender).Name = 'Unloadresources1') or // PopupActionBar1.Items[3]
(TComponent(Sender).Name = 'VKPCache11') or // PopupActionBar1.Items[3].Items[0]
(TComponent(Sender).Name = 'VKPCache21') or // PopupActionBar1.Items[3].Items[1]
(TComponent(Sender).Name = 'AllCache31') or // PopupActionBar1.Items[3].Items[2]
(ActLoadVal = 2)) and (PopupActionBar1.Items[3].Caption = 'Delete VKP Cache') then begin .. end;
end;
问题在于,如果程序用户想要在运行时添加/拖放/插入组件或控件或对象,这是一种较弱的方法并且需要额外的编码。这样,程序本身会自动代替我完成工作-知道要调用什么以及何时调用:)
在(静态)Form1 是(静态)PopupActionBar1。它有四个项目。第四个项目有子菜单 - 三个项目。
带有子菜单项(PopupActionBar1.Items[3])的第四项和三个子菜单项(PopupActionBar1.Items[3].Items[0 .. 2] OnClick 事件处理程序都设置为包含 If..Then 语句的过程以上。
任务 - 通过评估 Sender 参数并使用其 OOP 功能 - 检查是否已从 PopupActionBar1.Items[3] 菜单项或其子菜单项( PopupActionBar1.Items[3].Items[0] 或 PopupActionBar1.Items [3].Items[1] 或 PopupActionBar1.Items[3].Items[2] )。
我尝试了各种语法...还尝试使用 TControl、TWinControl、TComponent 进行类型转换操作 ..(不使用 TObject 是因为它没有父级(不包括 OLE)..
【问题讨论】:
-
您的代码驻留在哪个事件中?另外,我不确定您要达到的目标。能详细点吗?
-
以什么方式?代码片段?图表?
-
请添加更详细的代码,因为您的问题(基于 Sertac 回答下的讨论)似乎比现在制定的要复杂得多。
-
相信!需要更多信息?
-
不提供答案以免误解
,但您确实不应该将处理程序附加到“UnloadResources1”(除非您计划即时修改子项),处理程序将在显示子菜单,然后在用户进行选择时再次触发。除此之外,我会去“如果(Sender is TMenuItem)和(TMenuItem(Sender).Parent = UnloadResources1)那么”。
标签: delphi controls parent menuitem sender