Solved

Steps of creating a simple component

Posted on 2002-04-22
3
138 Views
Last Modified: 2010-04-04
Hi Experts,

I am new to writing Delphi component, and would like to learn the STEPS of creating components.

What I would like to do is to add code to the standard button, so that when the user click on the button, it will trigger: beep; (or showMessage('Hi');). The following steps are what I have done:

1. close all projects, click File->New->Others->components

2. In the new component dialog box select:
   Ancestor type = TButton[StdCtrls]
   Class name = testing
   Pallete page = Samples
   Unit file name = 'H:\Delphi\01\component2\Button1.pas'
   Search path = default

3. Click OK

4. Now I am in a unit file, showing as below:

{code as below}
unit Button1;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
  TButton1 = class(TButton)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('samples', [TButton1]);
end;

end.

Question:
1. Where should I add the beep, or showMessage('Hi');?
2. In additional to 'dclusr.dpk', can we create own dpk files?
3. I have tried using showMessage('Hi'); and it has an error. Does it mean that I have to add 'use dialogs' in the uses section?


Thank you very much in advance!
0
Comment
Question by:ourselves
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
DrDelphi earned 100 total points
ID: 6961788
Modify your code to look something like this:

uses Dialogs;/// for ShowMesssage
type
 TButton1 = class(TButton)
 private
   { Private declarations }
 protected
   Procedure BeepMe(Sender:Tobject);
   { Protected declarations }
 public
   { Public declarations }
   Constructor Create(Aonwer:Tcomponent);Override;
published
   { Published declarations }
 end;

procedure Register;

implementation

procedure Register;
begin
 RegisterComponents('samples', [TButton1]);
end;


Constructor TButton1.create(Aowner:Tcomponent);
begin
 inherited
 Self.OnClick:=BeepMe;
end;

Procedure TButton1.BeepMe(Sender:TObject);
begin
  Showmessage('Beep');
end;

end.

>In additional to 'dclusr.dpk', can we create own dpk
files?


Absolutely... Component/NewComponent/into new package from the main menu of the IDE snd specify a package name (ending .DPK)


>I have tried using showMessage('Hi'); and it has an error. Does it mean that I have to add 'use dialogs'
in the uses section?


Yup!

Good luck!!



0
 

Author Comment

by:ourselves
ID: 6961823
Thank you DrDelphi,

I like the answer, nice and simple, and it's just what I want.

Thanks again for your help!
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6961831
Cheers.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

825 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