Question

I have a custom component (TScrollBox) that when dropped on a form, it will add a label inside the ScrollBox. How can I disable the ScrollBox's events (onClick, OnMouseDown, ect..) and instead enable the events for the child (Tlabel)

unit MyScrollBox;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls;

type
  TMyScrollComponent = class(TScrollBox)
  private
    FLabel : TLabel;
    procedure SetLabelText(AText : string);
    function GetLabelText : string;
  protected
    constructor Create(AOwner : TComponent); override;
  published       
    property LabelText : string read GetLabelText write SetLabelText;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TMyScrollComponent]);
end;

constructor TMyScrollComponent.Create(AOwner : TComponent);
begin
  inherited;
  FLabel := TLabel.Create(self);
  FLabel.Parent := self;
  FLabel.Caption := 'Hello From Scrollbox!';
end;

procedure TMyScrollComponent.SetLabelText(AText : string);
begin
  FLabel.Caption := AText;
end;

function TMyScrollComponent.GetLabelText : string;
begin
  result := FLabel.Caption;
end;

end.
Was it helpful?

Solution

The published events in TScrollBox cannot be suppressed in derived classes. So, treating your question literally, there is no way to achieve what you ask.

What you could do is derive from TScrollingWinControl. This is the ancestor to TScrollBox. It doesn't publish the events that you want to associate with the control contained in your scroll box.

Then you can surface events in your custom control that are connected to the control contained in your custom control.

Judging from your recent questions I cannot help in thinking that your approach is wrong. I feel that you should have a custom control that has built in scrolling capability.

OTHER TIPS

The event handlers for TControls are declared as protected and dynamic. Redeclare them using the override directive in your derived class - see TScrollBox Members Protected Methods;
To override MouseDown, add the MouseDown method to the TDBCalendar class and many other pages.

But: If you want to implement your own new events you'd have to do something like this:

   ...

    private

    fNewEvent:TNotifyEvent;

    procedure setNewEvent(notify:TNotifyEvent);
    function  getNewEvent:TNotifyEvent;

    procedure DoOnNewEvent;


    ....

    published 

    property OnNewEvent:TNotifyEvent read getNewEvent write setNewEvent;

i.e. - You need to implement a property of a method type, like TNotifyEvent which is built into Delphi. You can also create your own if you need to. If you want to see your event in the IDE like other Delphi components' events, you must declare it as published.

Then: In your new component implementation section do something like this:

procedure TMyclass.DoOnNewEvent;
begin

  if assigned (fNewEvent) then
   begin
   ....doStuff... 
      fNewEvent(self);
   end;

end;

You call DoOnNewEvent when the event that you want to control 'happens' in your code, so that the function assigned to fNewEvent will get called at that point in your code. (This is commonly known as a callback - when something "happens" in module A it calls back into module B letting it know that it happened, etc.)

If you want to define new GUI behavior, you have to examine the controls you're interested in and understand how to capture their actual "physical" events - i.e. when did the scrollbar scroll, when was the mouse clicked, and when that happens you call your DoOnNewEvent method. (This generally involves inspecting Windows messages coming into your application, "message cracking",etc - these messages inform your application of what's happening in "the outside world".)

In your consumer class, for example your main form where you're putting your scroll box, once you successfully publish your new event, you will see your event in the IDE on your new component, and you assign it and define the behavior you want for it in your consumer class, just like any other event in the IDE.

Take a look at the VCL source code for a simple component to get a better idea of what it looks like.

But: That's only if you really need your own new published events because overriding the parent's events is not sufficient for your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top