Board index » delphi » Handling multiple objects with one routine

Handling multiple objects with one routine

Hello !

I wonder about handling for example the clicking on several different objects with one and the
same procedure in Delphi 2.

If I for example have, say 2 or more components of type TPanel and wants the
same thing to be done, each time on of these is clicked upon, but I want to be able to determine
which panel is clicked on. Has anyone got any suggestions how to do this ?

I can do this by defining the same procedure to be executed when I click upon one of these
objects, but how can I determine which component is clicked upon ? I've got a feeling that by
looking into the API I'll get the answer, but I've got poor documentation.

If anyone has got suggestions, please e-mail me.

TIA

G Stromsten

e-mail: gstroms...@ngs1.pp.fi

 

Re:Handling multiple objects with one routine


Quote
Goran Stromsten (gstroms...@ngs1.pp.fi) wrote:

: Hello !

: I wonder about handling for example the clicking on several different objects with one and the
: same procedure in Delphi 2.

: If I for example have, say 2 or more components of type TPanel and wants the
: same thing to be done, each time on of these is clicked upon, but I want to be able to determine
: which panel is clicked on. Has anyone got any suggestions how to do this ?

Try:

procedure TForm1.PanelOnClick(Sender: TObject);
begin
  { Hard coded object recognition }
  if Sender = Panel1 Then ShowMessage('First Panel Selected');
  if Sender = Panel2 Then ShowMessage('Second Panel Selected');

  { Slightly more flexible recognition }
  if Sender is TPanel Then
    with Sender as TPanel do ShowMessage('Panel Called '+Name+' Selected');
end;

Hope this is of help,

 TTFN
        Eric Kendrick

--
                                Eric Kendrick // e...@oasis.icl.co.uk

Re:Handling multiple objects with one routine


Quote
Goran Stromsten wrote:

> Hello !

> I wonder about handling for example the clicking on several different objects with one and the
> same procedure in Delphi 2.

> If I for example have, say 2 or more components of type TPanel and wants the
> same thing to be done, each time on of these is clicked upon, but I want to be able to determine
> which panel is clicked on. Has anyone got any suggestions how to do this ?

> I can do this by defining the same procedure to be executed when I click upon one of these
> objects, but how can I determine which component is clicked upon ? I've got a feeling that by
> looking into the API I'll get the answer, but I've got poor documentation.

> If anyone has got suggestions, please e-mail me.

> TIA

> G Stromsten

> e-mail: gstroms...@ngs1.pp.fi

This is actually very simple.  Every event handler procedure takes a
parameter called "Sender".  This is the object which triggered the
event.

e.g. Imagine a form which demonstrated a gradient fill routine, which
used two panels to show the color to shade from and the color to shade
to, and the user simply clicks on each panel to change the required
color.  Each panel references OnColorPanelClick as its OnClick event
handler:

   procedure TMyForm.OnColorPanelClick(Sender: TObject; ... ; ...) ;
   begin
      dlgColor.Color := TPanel(Sender).Color ;
      if dlgColor.Execute then begin
          TPanel(Sender).Color := dlgColor.Color ;
          RepaintGradientFillArea ;
      end ;
   end ;

If you needed to make an explicit check for which panel had triggered
the event:

      if Sender = panelFromColor then begin
         :
      end else begin
         :
      end ;

Where panelFromColor is the object reference in your form to the panel
component.

I hope this is helpful.

Regards,

Jolyon.

Re:Handling multiple objects with one routine


Quote
Goran Stromsten wrote:

> Hello !

> I wonder about handling for example the clicking on several different objects with one and the
> same procedure in Delphi 2.

> If I for example have, say 2 or more components of type TPanel and wants the
> same thing to be done, each time on of these is clicked upon, but I want to be able to determine
> which panel is clicked on. Has anyone got any suggestions how to do this ?

> {SNIP}

> TIA

> G Stromsten

> e-mail: gstroms...@ngs1.pp.fi

Set the OnClick event handler for both (or all) of the components to the same method.  For example,
say you have three TPanels.  Go to the property page for one of the components and type in the name
of an event handler ( such as 'GenericTPanelClick' ).  Then select the other components and set
their OnClick also to 'GenericTPanelClick'.  Then inside the method, use the Sender parameter to
determine which panel to update.  If you wanted to change the panel's visibility for example, use
the following:

procedure TForm1.GenenricTPanelClick( Sender : TObject );
begin
    with ( Sender as TPanel ) do
    begin
        Visible := FALSE;
    end;
end;

Hope this helps,
Blake Schwendiman

Other Threads