faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 13 people (92%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Delphi: Component: Dynamic: Parameter: Pass: How to pass component name as a parameter to a method?

Sep 12th, 2003 09:33
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 12 September 2003 - 05:50 - 06:32 pm ----------

Delphi: Component: Dynamic: Parameter: Pass: How to pass component 
name as a parameter to a method?

To send the name of (your newly created) editbox to some general 
method (procedure / function),
you could use the following:

Steps: Overview:

 1. -Open a new form

 2. -Add the below procedure to your source code

procedure TForm1.PROCMyProcedureWithEditBoxName( myeditboxnameS : 
string );
var componentT : TComponent;
begin
 componentT := FindComponent( myeditboxnameS ) as TEdit;
 if Assigned( componentT ) then begin
  with componentT as TEdit do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + myeditboxnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( myeditboxnameS + ' ' + 'not found' );
 end;
end;

 3. -Put a button on it

 4. -Add an editbox to it

 5. -Double click on the button and add the following code:

      PROCMyProcedureWithEditBoxName( 'Edit1' );

 6. -That will alltogether give the following code:

--- cut here ---------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure PROCMyProcedureWithEditBoxNameAsParameter( 
myeditboxnameS : string );
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.PROCMyProcedureWithEditBoxNameAsParameter( 
myeditboxnameS : string );
var componentT : TComponent;
begin
 componentT := FindComponent( myeditboxnameS ) as TEdit;
 if Assigned( componentT ) then begin
  with componentT as TEdit do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + myeditboxnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( myeditboxnameS + ' ' + 'not found' );
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 PROCMyProcedureWithEditBoxNameAsParameter( 'Edit1' );
end;

end.

--- cut here ---------------------------------------------------------

 7. If you run this code, you will first see a button
    and an editbox.

  +------------------+
  |                  |
  +------------------+

  +------------------+
  | Edit1            |
  +------------------+


 8. If you click on the button it will show a message box
    with 'Hello World' and the name of the component which
    called it (e.g. 'Edit1', or if you should put another
    component on it, e.g. EditBox2, and you call it
    with 'Edit2', it will show you that information instead.

    So thus you pass the name of your Edit component dynamically
    as a parameter to that method.

  +------------------+   +------------------------+
  |                  |   |                        |
  +------------------+   |                        |
                         | Hello World from Edit1 |
  +------------------+   |                        |
  | Edit1            |   |                        |
  +------------------+   +------------------------+

---
---

Overview of possible methods of passing a component as a parameter
for some commonly used components, like for a label, listbox, ... are 
so:

---

---

Note: a similar method for a Button component would be:

procedure TForm1.PROCMyProcedureWithButtonNameAsParameter( 
mybuttonnameS : string );
// e.g. PROCMyProcedureWithButtonNameAsParameter( 'Button1' );
var componentT : TComponent;
begin
 componentT := FindComponent( mybuttonnameS ) as TButton;
 if Assigned( componentT ) then begin
  with componentT as TButton do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + mybuttonnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( mybuttonnameS + ' ' + 'not found' );
 end;
end;

---

Note: a similar method for an Editbox component would be:

procedure TForm1.PROCMyProcedureWithEditBoxNameAsParameter( 
myeditboxnameS : string );
// e.g. PROCMyProcedureWithEditBoxNameAsParameter( 'Edit1' );
var componentT : TComponent;
begin
 componentT := FindComponent( myeditboxnameS ) as TEdit;
 if Assigned( componentT ) then begin
  with componentT as TEdit do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + myeditboxnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( myeditboxnameS + ' ' + 'not found' );
 end;
end;

---

Note: a similar method for a Label component would be:

procedure TForm1.PROCMyProcedureWithLabelNameAsParameter( 
mylabelnameS : string );
// e.g. PROCMyProcedureWithLabelNameAsParameter( 'Label1' );
var componentT : TComponent;
begin
 componentT := FindComponent( mylabelnameS ) as TLabel;
 if Assigned( componentT ) then begin
  with componentT as TLabel do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + mylabelnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( mylabelnameS + ' ' + 'not found' );
 end;
end;

---

Note: a similar method for a Listbox component would be:

procedure TForm1.PROCMyProcedureWithListBoxNameAsParameter( 
mylistboxnameS : string );
// e.g. PROCMyProcedureWithListBoxNameAsParameter( 'ListBox1' );
var componentT : TComponent;
begin
 componentT := FindComponent( mylistboxnameS ) as TListBox;
 if Assigned( componentT ) then begin
  with componentT as TListBox do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + mylistboxnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( mylistboxnameS + ' ' + 'not found' );
 end;
end;


---


Note: A similar method for a Memo component would be:

procedure TForm1.PROCMyProcedureWithMemoNameAsParameter( mymemonameS : 
string );
// e.g. PROCMyProcedureWithMemoNameAsParameter( 'Memo1' );
var componentT : TComponent;
begin
 componentT := FindComponent( mymemonameS ) as TMemo;
 if Assigned( componentT ) then begin
  with componentT as TMemo do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + mymemonameS );
  end;
 end
 else begin
  // do something
  ShowMessage( mymemonameS + ' ' + 'not found' );
 end;
end;

---
---

Note: Passing an integer (e.g. 1) instead of the whole component name 
(e.g. 'Edit1') as a string

If you only want to pass a number (e.g. an integer) indicating the
number of the component (e.g. 'Edit1', 'Edit2', 'Edit3', ...),
you could use string concatenation and IntToStr() like in the
following:

procedure TForm1.PROCMyProcedureWithEditBoxNameAsParameter( I : 
integer );
// e.g. PROCMyProcedureWithEditBoxNameAsParameter( 1 );
var componentT : TComponent;
var s : string;
begin
 s := 'EditBox' + IntToStr( I );
 componentT := FindComponent( myeditboxnameS ) as TEdit;
 if Assigned( componentT ) then begin
  with componentT as TEdit do begin
   // do something
   ShowMessage( 'Hello world from' + ' ' + myeditboxnameS );
  end;
 end
 else begin
  // do something
  ShowMessage( myeditboxnameS + ' ' + 'not found' );
 end;
end;

----------------------------------------------------------------------