打开项目时,RAD Studio XE2删除dfm中的OnClick设置

时间:2012-10-24 09:03:05

标签: delphi ide delphi-xe2

我们有一个可以在简单或高级模式下运行的应用程序。不同之处在于主菜单条目可见。问题是RAD Studio XE2以静默方式删除dfm中设置的OnClick属性值。它指向隐藏主菜单中的一些菜单项的过程>文件菜单。

步骤:
1.从颠覆到磁盘上的新文件夹的新结账,这是以前从未存在的文件夹 2.在notepad ++中打开主窗体的dfm和pas文件,以确保存在“OnClick = MenuItem_File1Click”行,并且该过程MenuItem_File1Click位于pas文件的接口和实现中。是的,一切都很好 3.在RAD Studio XE2中打开项目 4.对象检查器中“文件”菜单项的“OnClick”属性为空。

我们在FormShow中处理了一些其他菜单项,它们按预期工作,以简单模式隐藏,在高级中可见。解决方法是将MenuItem_File1Click内的功能移动到FormShow。但我真的很想知道为什么它一开始就发生了。

任何人都可以解释这种行为吗?有没有其他人遇到这样的事情?我尝试将程序重命名为某种类似于系统的东西,但无论如何IDE都将其删除了。

1 个答案:

答案 0 :(得分:1)

如果您将Menu(或Button)与Action联系起来,那么Action应该接管所有这些属性: AutoCheck, Caption, Checked, Enabled, HelpContext, Hint, GroupIndex, Bitmap/ImageIndex, ShortCut, Visible以及 OnClick/Execute

这是“行动”中的“存在理由”。

因此,如果您离开Action而没有Execute事件处理程序,则事实上将nil放入Control的OnClick事件中。

当dfm中指定的Action挂钩到控件时,只要从dfm读取控件就会发生这种情况。只是尝试设置OnClick,然后切换到View As Text并返回(Alt + F12两次),你的OnClick就不见了......

参见VCL来源:

procedure TMenuItem.ActionChange(Sender: TObject; CheckDefaults: Boolean);
begin
  if Sender is TCustomAction then
    with TCustomAction(Sender) do
    begin
      if not CheckDefaults or (Self.AutoCheck = False) then
        Self.AutoCheck := AutoCheck;
      if not CheckDefaults or (Self.Caption = '') then
        Self.Caption := Caption;
      if not CheckDefaults or (Self.Checked = False) then
        Self.Checked := Checked;
      if not CheckDefaults or (Self.Enabled = True) then
        Self.Enabled := Enabled;
      if not CheckDefaults or (Self.HelpContext = 0) then
        Self.HelpContext := HelpContext;
      if not CheckDefaults or (Self.Hint = '') then
        Self.Hint := Hint;
      if RadioItem and (not CheckDefaults or (Self.GroupIndex = 0)) then
        Self.GroupIndex := GroupIndex;
      if not CheckDefaults or (Self.ImageIndex = -1) then
        Self.ImageIndex := ImageIndex;
      if not CheckDefaults or (Self.ShortCut = scNone) then
        Self.ShortCut := ShortCut;
      if not CheckDefaults or (Self.Visible = True) then
        Self.Visible := Visible;
      if not CheckDefaults or not Assigned(Self.OnClick) then
        Self.OnClick := OnExecute; // <====== use debug dcus and put a break here...
    end;
end;

更新:...但如果您在OnClick事件中有代码,则不会发生这种情况。
这看起来像一个bug。从dfm读取MenuItem时,父窗体尚未完全加载,OnClick显示为

Name            Value
FOnClick    ($3,$6142210)
    Code    $3
    Data    $6142210

Assigned(FOnClick)会返回False !!!!

所以if not CheckDefaults or (@Self.OnClick=nil) then 将是一个更好的测试