Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Steps of creating a simple component

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

715 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