Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Steps of creating a simple component

Posted on 2002-04-22
3
Medium Priority
?
156 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 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

782 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