Jump to content
UCT_24

Can Control Elements have "EventHandlers"?

Recommended Posts

Hello everybody,
 

i am wondering if a Control can have a specific EventHandler.

I mean the following: If a Control has a certain "Event", i can implement an Event Handler for the Form which holds the Control.
I wonder if it possible to implement the Event handler as a method of the Component class and thus, the Event is handled by each instance of this component on any Form i use this Component class on (without implementing an event handler on every Form for every Instance, hope it is possible to understand what i am aiming at.)

I have never seen such thing. If this is possible, can someone maybe advise me an example how this can be done?

 

Thanks a lot and kind regards,
UCT
 

Share this post


Link to post
2 hours ago, UCT_24 said:

Hello everybody,
 

i am wondering if a Control can have a specific EventHandler.

I mean the following: If a Control has a certain "Event", i can implement an Event Handler for the Form which holds the Control.
I wonder if it possible to implement the Event handler as a method of the Component class and thus, the Event is handled by each instance of this component on any Form i use this Component class on (without implementing an event handler on every Form for every Instance, hope it is possible to understand what i am aiming at.)

I have never seen such thing. If this is possible, can someone maybe advise me an example how this can be done?

 

Thanks a lot and kind regards,
UCT
 

If you look at the VCL source (if you have it) in the design the VCL follows each component having an event also has a virtual or dynamic method, usually protected, that fires the event. This method can be overridden in descendants to change the way the component handles the event. So, in your case, you would implement such a method to provide the default processing and optionally to also fire an associated event, if a handler has been assigned to it.

Share this post


Link to post

Thanks, so if i can identify the method corresponding to the event and override it, i can put some "custom code" there. 
I will try so.
Can you in addition tell me if i can access a components parent form in the method of the component? This would help me a lot.
Thanks

Share this post


Link to post
34 minutes ago, UCT_24 said:

Can you in addition tell me if i can access a components parent form in the method of the component? This would help me a lot.
Thanks

Without having more information:

 

- If a component is placed on the form using the IDE the owner of the component is going to be the form. This can be different when the component is created runtime.

- You can go using in the component hierarchy using the parent property until you find the form you are looking for.

 

 

 

Share this post


Link to post

1.  form := GetParentForm(AControl);

2. example  speedbutton click overridden to allow click to jump to control on another form. 

  TpfJumper = class(TSpeedButton)
  private
    FgotoName: string;
    FgotoControl: TControl;
    procedure SetgotoControl(const Value: TControl);
    procedure SetgotoName(const Value: string);
//    procedure onclick;

  protected
    procedure Paint; override;
  public
    property gotoControl: TControl read FgotoControl write SetgotoControl;
    property gotoName: string read FgotoName write SetgotoName;
  published
    { Published declarations }
    procedure Click; override;

  end;

3. The events for the Control. 

Procedure TpfJumper.Paint;
begin
  with Canvas do
  begin
    brush.Color := clMoneyGreen;
    Point(85,21),Point(17,0)]);
    polygon([Point(68,0),Point(68,7),Point(85,7),Point(85,34),Point(68,34),Point(68,41),                          Point(0,21),Point(68,0)]);
  end;
end;

procedure TpfJumper.click;
begin
  inherited;
  gotoControl.Show;
end;

4.  In the designer you can assign several controls to one event.  In the handler cast the sender as needed.       

Share this post


Link to post

Hello, thanks again for your reply.
It should be the first case:

1 hour ago, Lajos Juhász said:

- If a component is placed on the form using the IDE the owner of the component is going to be the form. This can be different when the component is created runtime.

So, i think this will help me out, thanks a lot and kind regards.

UTC

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×