Solved

Override method of component in a component.

Posted on 2006-06-19
3
300 Views
Last Modified: 2010-04-05
Ive got this component that im making, which is basically just a regular label, except when you double-click it, it turns into an edit box so that you can edit the caption of the label.  Anyways, my component descends from the TCustomLabel, and on the double click, it creates an Edit box, as so:

procedure TEditorLabel.DblClick;
begin
  // FEdit := TEdit.Create(Self.Owner);                                   <-- This is what is created in the constructor of the component....
  Self.Visible := False;
  FEdit.Left := Self.Left; FEdit.Top := Self.Top;
  FEdit.Text := Self.Caption;
  FEdit.Parent := Self.Parent;
end;

This works great, except that I want to override the OnKeyDown event of this new edit!  So I want to do

FEdit.OnKeyDown := ___________;

except, how would I do that?  It seems that I cant just create an event and assign it.... so what do I do?
0
Comment
Question by:cholin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 16939159
Yes, you can just assign an event at runtime.... just add it as a public/protected method of your class and make sure it matches the event type for OnKeyDown, eg:

type
  TEditorLabel      =  class(TLabel)
  private
     // Private declarations
     FEdit:         TEdit;
  protected
     // Protected declarations
     procedure      DblClick; override;
     procedure      OnKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  public
     // Public declarations
     constructor    Create(AOwner: TComponent); override;
  end;

--

constructor TEditorLabel.Create(AOwner: TComponent);
begin

  // Perform inherited
  inherited Create(AOwner);

  // Create edit
  FEdit:=TEdit.Create(Self);

  // Your code....

end;

procedure TEditorLabel.OnKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin

  //
  // Do whatever is required ...
  //

end;

procedure TEditorLabel.DblClick;
begin

  Self.Visible := False;
  FEdit.Left := Self.Left; FEdit.Top := Self.Top;
  FEdit.Text := Self.Caption;
  FEdit.Parent := Self.Parent;
  FEdit.OnKeyDown := Self.OnKeyDown;

end;


--

Regards,
Russell

0
 
LVL 1

Author Comment

by:cholin
ID: 16939335
Alright, it works now for the most part, except that when I close my program, it gives me access errors which means that Im not cleaning up properly....  any idea what's wrong with this code?

constructor TEditorLabel.Create(AOwner: TComponent);
begin
  inherited;
  FEdit := TEdit.Create(Self);
end;

destructor TEditorLabel.Destroy;
begin
  FEdit.OnKeyPress := nil;
  FEdit.Free;
  inherited;
end;

procedure TEditorLabel.DblClick;
begin
  Visible := False;
  FEdit.Left := Left - 2; FEdit.Top := Top - 2;
  FEdit.Text := Caption;
  FEdit.Parent := Parent;
  FEdit.OnKeyPress := OnKeyPress;
end;

procedure TEditorLabel.OnKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then begin
    Key := #0;
    Caption := FEdit.Text;
    FEdit.Visible := False;
    Visible := True;
  end;
end;

If I just open and close the application, it works just fine, meaning that the FEdit is getting created and destroyed just fine... so now why does it throw exceptions when I close the application after executing the DblClick event?  
0
 
LVL 1

Author Comment

by:cholin
ID: 16939347
Nevermind, turns out you can't call FREE if you pass a valid owner in the constructor (http://delphi.about.com/od/kbcurt/a/dynamiccreation.htm)
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question