Solved

Override method of component in a component.

Posted on 2006-06-19
3
278 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
  • 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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