【问题标题】:Firemonkey custom control click events firing in designerFiremonkey 自定义控件点击事件在设计器中触发
【发布时间】:2017-03-01 19:01:29
【问题描述】:

我有一个自定义的 Firemonkey 控件,它有几个子组件。这些子组件具有与其关联的 OnClick 事件,这些事件在控件的构造函数中设置。我注意到,当我在设计视图中单击自定义控件时,这些子组件的 OnClick 事件会被触发。

是否需要采用特定设置或最佳实践来防止这种情况发生?

有什么我可以在我的 C++ 代码中检查的东西,以查看此事件是在设计器中运行还是在运行时运行?比如:

void __fastcall MyControlOnClick( TObject * Sender )
{
    if( InDesigner == false )
    {
         //do stuff here
    }
}

【问题讨论】:

    标签: c++ c++builder firemonkey c++builder-10.1-berlin


    【解决方案1】:

    使用ComponentState 属性。当您在表单设计器中使用您的控件时,它会启用csDesigning 标志。

    void __fastcall MyControl::SubControlClick(TObject *Sender)
    {
        if( !ComponentState.Contains(csDesigning) )
        {
             //do stuff here
        }
    }
    

    或者,不要在设计时分配 OnClick 处理程序:

    __fastcall MyControl::MyControl(TComponent *Owner)
        : TBaseControl(Owner)
    {
        ...
        FSubControl = new TWhatever(this);
        if( !ComponentState.Contains(csDesigning) )
            FSubControl->OnClick = &SubControlClick;
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多