Solved

Steps of creating a simple component

Posted on 2002-04-22
3
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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