Solved

Steps of creating a simple component

Posted on 2002-04-22
3
131 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
Comment Utility
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
Comment Utility
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
Comment Utility
Cheers.
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

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

17 Experts available now in Live!

Get 1:1 Help Now