Jump to content
Stano

Custom Component: to create an event for a field of type TBaseVirtualTree

Recommended Posts

My component contains the field FVirtualTree: TBaseVirtualTree; FVirtualTree is not a subcomponent. There are examples of this. Assigned during design. I need to write an OnChange event for FVirtualTree in the component. It should work like this

When writing a component:

  • for FVirtualTree I will create OnChange. I'll write my code

At design runtime

  • Assign to component (property VirtualTree) Vst1 (TVirtualStringTree)
  • For Vst1 I define the OnChange event

During the runtime

  • I change the node - it calls Vst1.OnChange
  • I'll do something there
  • I call FVirtualTree.OnChange
  • Vst1.OnChange completes

I did not find anything about such a case.

Share this post


Link to post

Is this what you are looking for?

type
  TMyComponent = class(...)
  private
    FVirtualTree: TBaseVirtualTree;
    procedure SetVirtualTree(AValue: TBaseVirtualTree);
    procedure VirtualTreeChange(Sender: TBaseVirtualTree; Node: PVirtualNode);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    destructor Destroy; override;
  published
    property VirtualTree: TBaseVirtualTree read FVirtualTree write SetVirtualTree;
  end;

destructor TMyComponent.Destroy;
begin
  SetVirtualTree(nil);
  inherited;
end;

procedure TMyComponent.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FVirtualTree) then
    FVirtualTree := nil;
end;

procedure TMyComponent.SetVirtualTree(AValue: TBaseVirtualTree);
begin
  if AValue <> FVirtualTree then
  begin
    if FVirtualTree <> nil then
    begin
      FVirtualTree.OnChange := nil;
      FVirtualTree.RemoveFreeNotification(Self);
    end;
    FVirtualTree := AValue;
    if FVirtualTree <> nil then
    begin
      FVirtualTree.FreeNotification(Self);
      FVirtualTree.OnChange := VirtualTreeChange;
    end;
  end;
end;

procedure TMyComponent.VirtualTreeChange(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
  // do something...
end;

 

Edited by Remy Lebeau

Share this post


Link to post

Unfortunately, it's not good. It behaves as if I simply / directly assigned the event to VST. It means:
the TjstVstDBNavigator.VirtualTreeChange procedure is called (Sender: TBaseVirtualTree; Node: PVirtualNode);
MyVst.OnChange is NOT called

Share this post


Link to post
7 hours ago, Stano said:

Unfortunately, it's not good. It behaves as if I simply / directly assigned the event to VST. It means:
the TjstVstDBNavigator.VirtualTreeChange procedure is called (Sender: TBaseVirtualTree; Node: PVirtualNode);
MyVst.OnChange is NOT called

Correct, because a component event can have only 1 handler assigned at a time.  If you need multiple handlers, you need to chain them together, ie have one handler call the other.  After re-reading your original post, it is not clear to me what exactly you really want, so please clarify, preferably with an actual workflow example.

Share this post


Link to post

You brought me the following solution, which I originally rejected. That's not how I imagined it. It's the easiest.
Thanks!

procedure TfrmMainForm.jstvst1Change(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
  NodeData: PAddress;
begin
  if not Assigned(Node) then
    Exit;

  NodeData := jstvst1.GetNodeData(Node);
  jstnav1.PKeyValue := NodeData.PKey;
  jstnav1.VirtualTreeChange(Sender, Node);  //  I thought he would be inherited here

 

Share this post


Link to post
46 minutes ago, Stano said:

You brought me the following solution, which I originally rejected. That's not how I imagined it. It's the easiest.

That new snippet has nothing to do with your proposed VirtualTree property.  So now I am completely lost as to what you really want.  I'm out.  Good luck.

 

Share this post


Link to post

I gave up the original idea. What I asked here.

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

×