C ++ Builder事件处理程序错误

时间:2014-08-21 18:11:01

标签: event-handling c++builder firemonkey

我是C ++ Builder的新手,并且对于专家来说很明显的语法很难。

在我的代码中,我创建了一个TImage,并希望为其OnItemClick事件分配一个ListBox OnClick处理程序。任何帮助将不胜感激!

void __fastcall TDetail::Button1Click(TObject *Sender)
{
    Detail->DataSource1->DataSet->Open();
    lynCount = Detail->DataSource1->DataSet->RecordCount;

    for(int lyn = 1; lyn <= lynCount; lyn++)
    {
        try {delete Pic[lyn];} __finally {Pic[lyn] = new TImage(Detail->BordLys);}
        Pic[lyn]->OnClick = TDetail::BordLysItemClick; 
    }
}

我收到错误:

  

[bcc32错误] Boom.cpp(262):E2034无法转换&#39; void(_fastcall *(_ enclosure)(const TCustomListBox *,const TListBoxItem *))(const TCustomListBox *,const TListBoxItem *)&#39 ;到&#39; TNotifyEvent&#39;

也尝试了这个没有成功:

void __fastcall TDetail::BordLysItemClick(const TCustomListBox *Sender, const TListBoxItem *Item)
{
    Debug->Text =  BordLys->ItemIndex + 1;
}

void __fastcall TDetail::Button1Click(TObject *Sender)
{
    Detail->DataSource1->DataSet->Open();
    lynCount = Detail->DataSource1->DataSet->RecordCount;

    BordItem = new TListBoxItem(Detail->BordLys);

    for(int lyn = 1; lyn <= lynCount; lyn++)
    {
        try {delete Pic[lyn];} __finally {Pic[lyn] = new TImage(Detail->BordLys);}
        BordItem = Detail->BordLys->ItemByIndex(lyn);
        Pic[lyn]->OnClick = TDetail::BordLysItemClick(Detail->BordLys, BordItem);
    }
}

我收到错误:

  

[bcc32错误] Boom.cpp(116):E2283使用。或 - &gt;调用&#39; _fastcall TDetail :: BordLysItemClick(const TCustomListBox *,const TListBoxItem *)&#39;

1 个答案:

答案 0 :(得分:0)

BordLysItemClick()的签名与OnClick事件所期望的签名不匹配。因此,您必须使用间接解决方案:

void __fastcall TDetail::BordLysItemClick(const TCustomListBox *Sender, const TListBoxItem *Item)
{
    Debug->Text = Item->Index + 1;
}

void __fastcall TDetail::BordLysImgClick(TObject *Sender)
{
    TImage *Img = static_cast<TImage*>(Sender);
    TListBoxItem *Item = reinterpret_cast<TListBoxItem*>(Img->Tag);
    BordLysItemClick(BordLys, Item);
}

void __fastcall TDetail::Button1Click(TObject *Sender)
{
    DataSource1->DataSet->Open();
    lynCount = DataSource1->DataSet->RecordCount;

    for(int lyn = 1; lyn <= lynCount; lyn++)
    {
        try
        {
            delete Pic[lyn];
        }
        __finally
        {
            Pic[lyn] = new TImage(BordLys);
        }

        TListBoxItem *BordItem = BordLys->ItemByIndex(lyn);

        Pic[lyn]->Tag = reinterpret_cast<NativeInt>(BordItem);
        Pic[lyn]->OnClick = &BordLysImgClick;
    }
}