我是c++ Builder的新手,语法对专家来说是显而易见的。
在我的代码中,我创建了一个TImage
,并希望为其OnClick
事件分配一个ListBox OnItemClick
处理程序。任何帮助将非常感激!
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;
}
}
I get error:
[bcc32错误]boom.com cpp(262): E2034不能转换'void (_fastcall * (_closure)(const TCustomListBox *,const TListBoxItem *))(const TCustomListBox *,const TListBoxItem *)'到'TNotifyEvent'
也试过了,没有成功:
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);
}
}
I get error:
[bcc32 Error] Boom.cpp(116): E2283or ->调用'_fastcall TDetail::BordLysItemClick(const TCustomListBox *,const TListBoxItem *)'
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;
}
}