Board index » delphi » Turning a form into a (non-visual) component?

Turning a form into a (non-visual) component?

I have created a form which browses datasets.
It's self-contained and I'd like to turn it
into a non-visual component, which I could call
like the CommonDialog forms, ie.

  DataSetBrowser.Show(pDataSet : TDataSet);

I've never written a component, so I would
appreciate any hints as to how I'd go about
turning the form into a component.

- Morten

 

Re:Turning a form into a (non-visual) component?


It looks like you'll have to do something like this:

1. make your pDataSet as a published property, so it will be displayed
in the Object Browser.

TDataSetBrowserDlg = class(CommonDlg)
  private
    FDataSet: TDataSet;
  ...
  published
    property DataSet: TDataSet read FDataSet write FDataSet;
  ...
  end;

2. Foresee a public procedure Execute, to be used in your code at
runtime, like

procedure TDataSetBrowserDlg.Execute
begin
  ...
  with TDataSetBrowser.Create(Self) do
  begin
    pDataSet := FDataSet
    Show;
  end;
  ...
end;

3. Make sure your TDataSetBrowser handles the whole lifecycle of your
dialog, including Cancel/OK buttons

Good luck, Joeri

Quote
Morten Br?ten wrote:

> I have created a form which browses datasets.
> It's self-contained and I'd like to turn it
> into a non-visual component, which I could call
> like the CommonDialog forms, ie.

>   DataSetBrowser.Show(pDataSet : TDataSet);

> I've never written a component, so I would
> appreciate any hints as to how I'd go about
> turning the form into a component.

> - Morten

Re:Turning a form into a (non-visual) component?


Quote
Morten Br?ten wrote in message <369C8762.8A2FD...@statoil.com>...
>I have created a form which browses datasets.
>It's self-contained and I'd like to turn it
>into a non-visual component, which I could call
>like the CommonDialog forms, ie.

>  DataSetBrowser.Show(pDataSet : TDataSet);

>I've never written a component, so I would
>appreciate any hints as to how I'd go about
>turning the form into a component.

In the same unit as your form write a non-visual component that creates a
copy of your form as needed:

TNVC=class(Component)
  Procedure Show(DataSet : TDataSet);
end;

...

Procedure TNVC.Show;
begin
  With TMyForm.Create(Application) do try
    DoSomeSetup;
    ShowModal;
  finally
    free;
  end;
end;

And then make sure a copy of your form is not in the project auto-create
list.

Sam

Re:Turning a form into a (non-visual) component?


Turning forms into components is very easy and a good place to start
learning components. Just create a component the inherits from TComponent
(using the new component wizard in the component menu). Then just add a
method called Show (although Execute is probably better as it is the
standard), which creates and shows the form. Very simple.

Craig.

Quote
Morten Br?ten wrote in message <369C8762.8A2FD...@statoil.com>...
>I have created a form which browses datasets.
>It's self-contained and I'd like to turn it
>into a non-visual component, which I could call
>like the CommonDialog forms, ie.

>  DataSetBrowser.Show(pDataSet : TDataSet);

>I've never written a component, so I would
>appreciate any hints as to how I'd go about
>turning the form into a component.

>- Morten

Other Threads