Link to home
Start Free TrialLog in
Avatar of lirtua
lirtua

asked on

Borland C++ Builder control array events

Having created a control array in Borland C++ Builder, how does one access the control which has generated a message.

// declarations
#define NUM_IMG 10
TImage * image[NUM_IMG];
void __fastcall MyClick(TObject* Sender);

// construction of the array, where Image1 is a design time image with bitmap loaded.
   for(int i = 0; i < NUM_IMG; ++i)
   {
      image[i] = new TImage(sbPictureScrollBox);
      image[i]->Parent = sbPictureScrollBox;
      image[i]->Picture = Image1->Picture;
      image[i]->AutoSize = true;
      image[i]->Top = i * 10;
      image[i]->Left = 10;
      image[i]->Visible = true;
      image[i]->Invalidate();

      image[i]->OnClick = MyClick;
   }

In the implementation of the event handler MyClick, how do I find out which control has generated the event?

Avatar of DrDelphi
DrDelphi

Add a line to include a Tag for each TImage:


     image[i]->Tag=i;


Next in the MyClick routine query that tag's value:


void __fastcall TForm1::MyClick(TObject *Sender)
{
 if (dynamic_cast<TImage*>(Sender)!=NULL);
 ShowMessage(dynamic_cast<TImage*>(Sender)->Tag);
}


Good luck!!





     

The control that has generated the event is sender argument in method
void __fastcall MyClick(TObject* Sender);
ASKER CERTIFIED SOLUTION
Avatar of thienpnguyen
thienpnguyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lirtua

ASKER

Your answer is good.  It made me realize what Sender is. (something the borland help files fell short of)  And so, here is my choice of implementation:

void __fastcall TfrmMain::MyClick(TObject* Sender)
{
// get the control
// ok since this method is only for the image array
   TImage* img = (TImage*)Sender;

// mess with it
   img->Left += 10;
}