Solved

Override method of component in a component.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 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