Solved

Override method of component in a component.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi : could not find program, '...exe' 2 149
Printing problem 2 81
Convert GUI app into console app for Win32 Env 5 82
How to renew a Delphi rad-studio licence? 5 49
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

24 Experts available now in Live!

Get 1:1 Help Now