2

I am attempting to create a custom Firemonkey control which inheirts from the TListView control. I want to add some functionality to the control which is automatically executed when a user clicks on the control. Therefore, my goal is NOT to specify an OnItemClick method on my control's form, but rather add functionality directly into the control itself.

I am struggling to comprehend what I need to do in order to tap into the on click handler for TListView. In my head, I imagine my solution would look something similar to this pseudo code:

//somewhere in the base TListView code
void __fastcall TListView::ClickHandler()
{
    //logic for handling a click on the list view
}

//somewhere in my custom list view control
void __fastcall TMyListView::ClickHandler()
{ 
    TListView::ClickHandler(); //call base click handler so all the normal stuff happens

    //my additional logic goes here
}

However, I cant seem to find anything in the documentation about what method I should attempt to override, or how I should be going about this at all.

I did find this information about calling the 'Click-event' handler. I set up a simple example like so:

void __fastcall TFmListView::Click()
{
    ShowMessage("This is the control's click");
}

This works fine, however according to the documentation:

If the user has assigned a handler to a control's OnClick event, clicking the control results in that method being called.

Therefore, any additional logic I placed in the Click() method of the control would be lost if one of the control's on click event properties was set.

What is the proper way to go about extending the functionality of what happens when a custom control is clicked?

4

1 回答 1

1

这是适合您的 C++Builder 解决方案。

这是类接口和实现:

class TMyListView : public TListView
{
protected:
    virtual void __fastcall DoItemClick(const TListViewItem *AItem);
};

...

/* TMyListView */

void __fastcall TMyListView::DoItemClick(const TListViewItem *AItem)
{
    // write here the logic that will be  executed
    // even if the OnItemClick handler is not assigned
    ShowMessage("Internal itemclick");

    // call the OnItemClick handler, if assigned
    TListView::DoItemClick(AItem);
}

然后在表单声明中声明该类的一个实例 TMyListView和必要的事件处理程序:

TMyListView *LV;
void __fastcall MyOnItemClick(const TObject *Sender, const TListViewItem *AItem);

这是事件处理程序和 LV 创建的实现:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    LV = new TMyListView(this);
    LV->Parent = this;
    LV->Position->X = 1;
    LV->Position->Y = 1;
    LV->Width = 100;
    LV->Height = 100;

    LV->Items->Add()->Text = "111";

    LV->OnItemClick = &MyOnItemClick;
}

void __fastcall TForm1::MyOnItemClick(const TObject *Sender, const TListViewItem *AItem)
{
    ShowMessage("Assigned itemclick"); //or any other event handler action
}

两条消息都将显示。

于 2015-10-30T15:37:34.217 回答