Solved

Override method of component in a component.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now