【问题标题】:DBGrid focus bug on MDIDBGrid 在 MDI 上的焦点错误
【发布时间】:2014-02-17 16:46:55
【问题描述】:

当我将它放在 MDIChildForm 中时,我遇到了 DbGrid 焦点错误。

要重新处理该错误:

  • 创建 MDI 应用程序
  • 在主窗体中,在其中放置一个面板和一个编辑
  • 创建 MDI 子窗体
  • 放置一个 DBGrid 并分配数据(多于 1 条记录)

现在,运行应用程序,然后按照以下步骤操作:

  • 单击网格,将焦点放在第一行
  • 点击编辑,将其聚焦
  • 现在尝试单击 dbgrid 的另一行。

错误:

  • dbgrid 没有收到焦点,什么也没有发生!

我正在使用 Delphi 7

有人可以帮我解决办法吗?

【问题讨论】:

    标签: delphi focus delphi-7 dbgrid


    【解决方案1】:

    问题是由 Form.ActiveControl 造成的。

    在这种情况下,MDI 子项在编辑焦点后将 DBGrid 保留为活动控件,因此在单击 dbgrid 后不会调用 Windows.SetFocus。

    我通过覆盖 TDBGrid.SetFocus 解决了这个问题:

    type
        TMyDBGrid = class(TDBGrid)
        public
           procedure SetFocus; override;
        end;
    
    procedure TMyDBGrid.SetFocus;
    var
      form: TCustomForm;
    begin
      inherited;
    
      // BUG-FIX: force the SetFocus if the current Control is Self but not focused!
      form := GetParentForm(Self);
      if (form <> nil) and (form.ActiveControl = Self) and not Focused then
        Windows.SetFocus(Self.Handle);
    end;
    

    【讨论】:

    • +1 很好找到。但我会调查一个更通用的解决方案,因为这个错误不仅仅适用于 DBGrid。
    【解决方案2】:

    我把问题解决了

    self.setfocus2
    

    在事件OnShow。我还在OnActivate 事件中添加了相同的代码。现在完美运行。

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多