We help IT Professionals succeed at work.

Borland C++ Builder control array events

lirtua
lirtua asked
on
Medium Priority
2,525 Views
Last Modified: 2007-12-19
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?

Comment
Watch Question

Commented:
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);
The control that has generated the event is sender argument in method
                     void __fastcall MyClick(TObject* Sender);

void __fastcall TForm1::MyClick(TObject *Sender)
 {
      int i;
      for(int i = 0; i < NUM_IMG; ++i)
               if(  Sender ==   image[i]  )
                     break;
     if( i < NUM_IMG )
     {
               // image[i] generated event
     }

}

Author

Commented:
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;
}