Couple of ways to do this:
1. Own Constructur
2. Properties
3. Class Method Activation (Sample already supplied)
1. Own Constructur
With a form such like:
type
TForm2 = class(TForm)
Label1 : TLabel;
private
{ Private declarations }
public
constructor MyCreate(AOwner : TComponent; AParam : string);
end;
implementation
{$R *.DFM}
constructor TForm2.MyCreate(AOwner : TComponent; AParam : string);
begin
inherited create(AOwner);
self.label1.caption := AParam;
end;
And called:
var
f2 : TForm2;
begin
f2 := Tform2.MyCreate(nil, 'MyString');
try
f2.ShowModal;
finally
FreeAndNil(f2);
end;
end;
2. Properties
type
TForm2 = class(TForm)
Label1 : TLabel;
procedure FormShow(Sender : TObject);
private
FMyProp : string;
public
property MyProp : string read FMyProp write FMyProp;
end;
var
Form2 : TForm2;
implementation
{$R *.DFM}
procedure TForm2.FormShow(Sender : TObject);
begin
Label1.Caption := FMyProp;
end;
procedure TForm1.Button1Click(Sender : TObject);
var
f2 : TForm2;
begin
Application.CreateForm(TForm2, f2);
try
f2.MyProp := 'Hello There';
f2.ShowModal;
finally
FreeAndNil(f2);
end;
end;
HTH's
At 16:37 22/06/2001 +1200, you wrote:
A simple question?
I hate global variables. They have their uses but I try and avoid them like
the plague if I can.
What I want to do is open a form with and pass it the ID of a record from a
table so that it will show only that record.
I do it like this now
NoteID := DBGrid2.SelectedField.asString; // where NoteID is a global
variable
Application.CreateForm(TNoteForm, NoteForm);
NoteForm.ShowModal;
NoteForm.Free;
when NoteForm opens it runs this...
procedure TNoteForm.FormCreate(Sender : TObject);
Query1.SQL.Clear;
Query1.SQL.Add('SELECT * FROM NOTES WHERE NOTEID = :NOTEID');
Query1.Params[0].asString := NoteID; // that Global variable
Query1.Open;
etc.
I can do it like this
Application.CreateForm(TNoteForm, NoteForm);
NoteForm.Label11.Caption := DBGrid2.SelectedField.asString;
NoteForm.ShowModal;
NoteForm.Free;
but this means I have an invisible label sitting on my form. Messy.
I have tried...
Application.CreateForm(TNoteForm, NoteForm);
NoteForm.NoteID := DBGrid2.SelectedField.asString;
NoteForm.ShowModal;
NoteForm.Free;
***
unit UNoteForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask, DBCtrls, Db, DBTables, Main, ComCtrls, PGlobal;
type
TNoteForm = class(TForm)
Label1: TLabel;
....
procedure FormCreate(Sender: TObject);
....
private
{ Private declarations }
public
{ Public declarations }
end;
var
NoteForm : TNoteForm;
NoteID : String;
doesn't work...
I am sure this is really simple and probably a basic concept in Delphi but I
have never been able to quite get it.
Thanks in advance,
Steve
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
----------------------------------------------------------------------
Donovan J. Edye [www.edye.wattle.id.au]
Namadgi Systems [www.namsys.com.au]
Voice: +61 2 6285-3460
Fax: +61 2 6285-3459
TVisualBasic = Class(None);
Heard just before the 'Big Bang': "...Uh Oh...."
----------------------------------------------------------------------
GXExplorer [http://www.gxexplorer.org] Freeware Windows Explorer
replacement. Also includes freeware delphi windows explorer components.
----------------------------------------------------------------------