Recent

Author Topic: Overriding of click method don't work in TreeView  (Read 397 times)

simsee

  • Full Member
  • ***
  • Posts: 205
Overriding of click method don't work in TreeView
« on: March 08, 2025, 12:03:34 pm »
If I override the click method of a TButton, when the control is clicked, the code of that method is executed, as I expect:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TButton }
  13.  
  14.   TButton=class(StdCtrls.TButton)
  15.     procedure Click; override;
  16.   end;
  17.  
  18.   { TForm1 }
  19.  
  20.   TForm1 = class(TForm)
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     Button : TButton;
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TButton }
  36.  
  37. procedure TButton.Click;
  38. begin
  39.   inherited Click;
  40.   writeln('Click');
  41. end;
  42.  
  43. { TForm1 }
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. begin
  47.   Button:=TButton.Create(self);
  48.   Button.Parent:=self;
  49. end;
  50.  
  51. end.

If I override the click method of a TTreeView, when the control is clicked, the code of that method is not executed:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls;
  9.  
  10. type
  11.  
  12.   { TreeView }
  13.  
  14.   TreeView=class(ComCtrls.TTreeView)
  15.     protected
  16.       procedure Click; override;
  17.   end;
  18.  
  19.   { TForm1 }
  20.  
  21.   TForm1 = class(TForm)
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.     TreeView : TTreeView;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TreeView }
  37.  
  38. procedure TreeView.Click;
  39. begin
  40.   inherited;
  41.   writeln('TreeView.Click');
  42. end;
  43.  
  44. { TForm1 }
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. var
  48.   TreeNode : TTreeNode;
  49. begin
  50.   TreeView:=TTreeView.Create(self);
  51.   TreeView.Parent:=self;
  52.   TreeView.Align:=alClient;
  53.   TreeNode:=TTreeNode.Create(TreeView.Items);
  54.   TreeView.Items.Add(TreeNode,'ok1');
  55.   TreeNode:=TTreeNode.Create(TreeView.Items);
  56.   TreeView.Items.Add(TreeNode,'ok2');
  57. end;
  58.  
  59. end.  

What is the reason for this different behavior? Thanks.

jamie

  • Hero Member
  • *****
  • Posts: 6897
Re: Overriding of click method don't work in TreeView
« Reply #1 on: March 08, 2025, 04:01:25 pm »
U never asigned the event so it's not going to be called. As for the button. U most likely used the OI to assign it.
The only true wisdom is knowing you know nothing

simsee

  • Full Member
  • ***
  • Posts: 205
Re: Overriding of click method don't work in TreeView
« Reply #2 on: March 08, 2025, 04:59:29 pm »
No. In both cases controls are created at run-time, wiithout using object inspector at all.

I attached two compilable projects to reproduce.

BrunoK

  • Hero Member
  • *****
  • Posts: 698
  • Retired programmer
Re: Overriding of click method don't work in TreeView
« Reply #3 on: March 08, 2025, 05:19:07 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls;
  9.  
  10. type
  11.  
  12.   { TreeView }
  13.  
  14.   TMyTreeView=class(ComCtrls.TTreeView)
  15.     protected
  16.       procedure Click; override;
  17.   end;
  18.  
  19.   { TForm1 }
  20.  
  21.   TForm1 = class(TForm)
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.     TreeView : TMyTreeView;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TreeView }
  37.  
  38. procedure TMyTreeView.Click;
  39. begin
  40.   inherited;
  41.   writeln('TreeView.Click');
  42. end;
  43.  
  44. { TForm1 }
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. var
  48.   TreeNode: TTreeNode;
  49. begin
  50.   TreeView := TMyTreeView.Create(self);
  51.   TreeView.Parent := self;
  52.   TreeView.Align := alClient;
  53.   TreeNode := TTreeNode.Create(TreeView.Items);
  54.   TreeView.Items.Add(TreeNode, 'ok1');
  55.   TreeNode := TTreeNode.Create(TreeView.Items);
  56.   TreeView.Items.Add(TreeNode, 'ok2');
  57. end;
  58.  
  59. end.

simsee

  • Full Member
  • ***
  • Posts: 205
Re: Overriding of click method don't work in TreeView
« Reply #4 on: March 08, 2025, 05:52:43 pm »
This was a simple typo: TreeType instead of TTreeType in line #14.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls;
  9.  
  10. type
  11.  
  12.   { TreeView }
  13.  
  14.   TTreeView=class(ComCtrls.TTreeView)
  15.     protected
  16.       procedure Click; override;
  17.   end;
  18.  
  19.   { TForm1 }
  20.  
  21.   TForm1 = class(TForm)
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.     TreeView : TTreeView;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TreeView }
  37.  
  38. procedure TreeView.Click;
  39. begin
  40.   inherited;
  41.   writeln('TreeView.Click');
  42. end;
  43.  
  44. { TForm1 }
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. var
  48.   TreeNode : TTreeNode;
  49. begin
  50.   TreeView:=TTreeView.Create(self);
  51.   TreeView.Parent:=self;
  52.   TreeView.Align:=alClient;
  53.   TreeNode:=TTreeNode.Create(TreeView.Items);
  54.   TreeView.Items.Add(TreeNode,'ok1');
  55.   TreeNode:=TTreeNode.Create(TreeView.Items);
  56.   TreeView.Items.Add(TreeNode,'ok2');
  57. end;
  58.  
  59. end.
  60.  

You don't need to use a different type name in the derived class definition. Sorry.
« Last Edit: March 08, 2025, 05:58:01 pm by simsee »

BrunoK

  • Hero Member
  • *****
  • Posts: 698
  • Retired programmer
Re: Overriding of click method don't work in TreeView
« Reply #5 on: March 08, 2025, 06:17:55 pm »
This was a simple typo: TreeType instead of TTreeType in line #14.
You don't need to use a different type name in the derived class definition. Sorry.
Your code does not compile. Message
Quote
unit1.pas(38,11) Error: Identifier not found "TreeView"
unit1.pas(38,11) Error: class identifier expected
unit1.pas(40,12) Error: Methods can be only in other methods called direct with type identifier of the class
     

Correct to

Code: Pascal  [Select][+][-]
  1. { TreeView }
  2.  
  3. procedure TTreeView.Click;
  4. begin
  5.   inherited;
  6.   writeln('TreeView.Click');
  7. end;

simsee

  • Full Member
  • ***
  • Posts: 205
Re: Overriding of click method don't work in TreeView
« Reply #6 on: March 08, 2025, 06:29:02 pm »
Yes, it is a consequence of the previous typo. I apologize.

BrunoK

  • Hero Member
  • *****
  • Posts: 698
  • Retired programmer
Re: Overriding of click method don't work in TreeView
« Reply #7 on: March 08, 2025, 06:37:32 pm »
It is not a good idea to 'façade' name an existing type with an identical class name. (except if you have a excellent reason to do it).

simsee

  • Full Member
  • ***
  • Posts: 205
Re: Overriding of click method don't work in TreeView
« Reply #8 on: March 08, 2025, 06:48:25 pm »
Yes, I agree, but the example comes from a refactoring context where I need an interceptor class for TTreeView. In this scenario, it makes sense.

 

TinyPortal © 2005-2018