Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TTreeView.OnChanging vs override...

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hi there,

How to override OnChanging event from within a VCL based on TTreeView??? I tried the 2 following declarations but doesn't work...

(msg 'method Changing not found in base class'...)
---------------------------------------------------
protected
{ Protected declarations }
1-procedure Changing(Node: TTreeNode; var AllowChange: Boolean); override;
2-function Changing(Node: TTreeNode): Boolean; override;
---------------------------------------------------

Thank you all
 
First step you have already done: Define your own procedure with the same parameter list as the Delphi event handler.

Second step, update the TreeView1 property to point to your version of the event handler:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView1.OnChanging := Changing;
end;
 
OK... but this way, the OnChanging will be captured (not be raised in each form implementing my TreeView VCL if I need some specific features) rather than override...

BTW, assigning OnChanging := Changing works, and that's the way I did it. But as I said, I would prefer to override the event instead of assign a replacement event handler...

Thanks for reply,
 
As the error says "Changing" method has not been defined in TTreeView or in any of its base classes so it cant be overriden. There is however a Change method you can override but overriding this will override your OnChange event behaviour and not OnChanging. To override the OnChanging event you need to override CanChange method instead.

The code below may explain better.

Code:
unit TreeView1;

interface

uses
  SysUtils, Classes, Controls, ComCtrls;

type
  TTreeView1 = class(TTreeView)
  protected
    procedure Change(Node: TTreeNode); override;
    function CanChange(Node: TTreeNode): Boolean; override;
  public
    { Public declarations }
  published
    { Published declarations }
  end;

procedure Register;

implementation
uses
  Dialogs;

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

procedure TTreeView1.Change(node:TTreeNode);
begin
    ShowMessage('Change override called');

    
    //If you want you can so the code below is optional 
    // do the default behaviour (call OnChange if assigned)
    inherited;
end;

function TTreeView1.CanChange(Node:TTreeNode):Boolean;
begin

  ShowMessage('Changing override called');

  //set the result to false if you dont want the change to happen for some
  //reason known to you
  result:=true;

  //If you want you can 
  //do the default behaviour (call OnChanging if assigned)
  if result then
    result:=inherited CanChange(Node);
end;

end.

If you are curious, I would recommend that you look at the definition and implementation of TCustomTreeView. Specifically pay attention to the following methods:
- TCustomTreeView.Change
- TCustomTreeView.Change;
- TCustomTreeView.CNNotify

"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
For this matter you may find it interesting to look at
- TCustomTreeView.CanChange and how it is called in TCustomTreeView.CnNotify

TCustomTreeView is defined in ComCtrls.pas
 
Thanks a lot bledazemi!
Exactly what I was looking for...:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top