Board index » delphi » Adding/ Removing Controls at run-time.

Adding/ Removing Controls at run-time.

I am designing a system which provides a TListView control from which the user
selects one of the items. depending on which item is selected I want to have
another control appear on the same Form. For example if they select a question
the new control would display the list of possible answers, if they select a
description the new control may display an image or bitmap... etc.

My question is how does one add and remove controls from a Form at run-time.

One option is to simply open and close a dialog box for each selected item...
this is not ideal for this application but if I can't figure out how to modify
the current Form dynamically then it will have to do.

--
David Sampson
samp...@inetworld.net

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

 

Re:Adding/ Removing Controls at run-time.


David,

There are two possibilities:

First, you can add all the possible controls to the form at design
time and set their Visible properties to False; when the user has
made their selection, simply set the appropriate control's Visible
property to True.

Or, create the new control dynamically:

var
  Memo: TMemo;
  Image: TImage;
begin  
   if ListView.Selected.Text = 'Question' then
   begin
      Memo := TMemo.Create(Self);
      with Memo do
      begin
         Parent := Self;
         Left := 100;
         // ... etc...
      end;
   end else if ListView.Selected.Text = 'Something for a bitmap' then
   begin
      Image := TImage.Create(Self);
      with Image do
      begin
           Parent := Self;
           // ... etc ...
      end;
   end;
end;

Ken
--
Ken White

Clipper Functions for Delphi
http://members.aol.com/clipfunc

Quote
dmsamp...@my-dejanews.com wrote:

> I am designing a system which provides a TListView control from which the user
> selects one of the items. depending on which item is selected I want to have
> another control appear on the same Form. For example if they select a question
> the new control would display the list of possible answers, if they select a
> description the new control may display an image or bitmap... etc.

> My question is how does one add and remove controls from a Form at run-time.

> One option is to simply open and close a dialog box for each selected item...
> this is not ideal for this application but if I can't figure out how to modify
> the current Form dynamically then it will have to do.

> --
> David Sampson
> samp...@inetworld.net

> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Re:Adding/ Removing Controls at run-time.


Quote
dmsamp...@my-dejanews.com wrote in message

<6tjrol$jc...@nnrp1.dejanews.com>...
Quote
>I am designing a system which provides a TListView control from which the
user
>selects one of the items. depending on which item is selected I want to
have
>another control appear on the same Form. For example if they select a
question
>the new control would display the list of possible answers, if they select
a
>description the new control may display an image or bitmap... etc.

>My question is how does one add and remove controls from a Form at

run-time.

I wouldn't consider adding and deleting controls at run time. It would be
far easier to place all the controls on the form, then use the Show and Hide
methods.  When I first started programming in delphi, my instinct was to
modify the Caption on a form, but that get's messy.

Re:Adding/ Removing Controls at run-time.


Quote
dmsamp...@my-dejanews.com wrote:
>My question is how does one add and remove controls from a Form at run-time.
>One option is to simply open and close a dialog box for each selected item...
>this is not ideal for this application but if I can't figure out how to modify
>the current Form dynamically then it will have to do.

Another 'easy' way would be to put a notebook control (the old 16-bit
style form works best for this) on the form, and display appropriate
pages (without page tabs) as needed.

However, that still uses resources and slows the program down, plus it
can make a very messy screen at design time. The unit below shows an
example of how to actually create forms and components on the fly at
run time, which is what you want to do.

Note - I just cut this material out of a large project of my own, so
this unit as it appears is untested. It should show you how to do what
you want, though...

**************************************

unit OnTheFly;

interface

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

type  // Form created on the fly at run-time
  TFM_RunTime = class(TForm)  

    RTButton : TButton;
    RTLabel  : TLabel;

    procedure RTButtonClick(Sender : TObject);

  end; // TFM_RunTime type declaration

type  // 'Normal' form made at design-time
  TFM_MyForm = class(TForm)  

    BT_Cancel: TButton;
    BT_MakeRunTime: TButton;

    procedure BT_MakeRunTimeClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { Private declarations }

  public
    { Public declarations }

  end;

var
  FM_MyForm  : TFM_MyForm;
  FM_RunTime : TFM_RunTime;

implementation

{$R *.DFM}

// OnClick method for runtime generated form/button
procedure TFM_RunTime.RTButtonClick (Sender : TObject);
begin

  Close;

end;

// In the OnCreate event of the design-time form, set up the
// run-time generated form. This could also be done in other
// places instead, such as the OnClick event for the
// BT_MakeRunTime button.
procedure TFM_MyForm.FormCreate(Sender: TObject);
begin

  // Create run time form
  FM_RunTime := TFM_RunTime.CreateNew(Application);
  with FM_RunTime do
  begin

    BorderStyle := bsDialog;
    Width := 300;
    Height := 350;
    Caption := 'This form generated at run time';

    // Add label component to form
    RTLabel := TLabel.Create(FM_RunTime);
    with RTLabel do
    begin

      Parent := FM_RunTime;  // Important to specify parent!
      Left := 9;
      Top := 15;
      Width := 300;
      Height := 16;
      Caption := 'This label created at run time';

    end;

    // Add button component to form
    RTButton := TButton.Create(FM_RunTime);
    with RTButton do
    begin

      Parent := FM_RunTime;
      Left := 183;
      Top := 288;
      Width := 100;
      Height := 29;
      Cancel := True;
      Caption := '&Runtime button';
      Default := True;
      Font.Charset := DEFAULT_CHARSET;
      Font.Color := clBtnText;
      Font.Height := -13;
      Font.Name := 'MS Sans Serif';
      Font.Style := [];
      ModalResult := 1;
      ParentFont := False;
      TabOrder := 0;
      // Note you can set methods as well as properties...
      OnClick := RTButtonClick;  

    end;

  end;

end;

// Button on design-time form that shows the run-time form
procedure TFM_MyForm.BT_MakeRunTimeClick(Sender: TObject);
begin

  FM_RunTime.ShowModal;

end;

end.

--
Kobayashi Software - Chicago, Illinois
www.kobayashi.com - e...@kobayashi.com

Re:Adding/ Removing Controls at run-time.


Quote
"Bob Richardson" <b...@whidbey.com> wrote:
>I wouldn't consider adding and deleting controls at run time. It would be
>far easier to place all the controls on the form, then use the Show and Hide
>methods.

While this would make things easier initially, it still ends up
requiring a lot of code to control the visibility of components at run
time.

It also can make for an annoyinglye messy form at design-time, and it
adds resource and processor time overhead to a program at run time.
There are a lot of situations where creating forms and components on
the fly at run time is preferable, especially if the items being
created are relatively simple.

Creating components, and even forms, at run time isn't really
difficult at all once you know the technique.

Quote
>When I first started programming in delphi, my instinct was to
>modify the Caption on a form, but that get's messy.

??? Modifying a caption requires one simple line of code.
--
Kobayashi Software - Chicago, Illinois
www.kobayashi.com - e...@kobayashi.com

Re:Adding/ Removing Controls at run-time.


Quote
> I am designing a system which provides a TListView control from which the
user
> selects one of the items. depending on which item is selected I want to
have
> another control appear on the same Form. For example if they select a
question
> the new control would display the list of possible answers, if they select
a
> description the new control may display an image or bitmap... etc.

> My question is how does one add and remove controls from a Form at
run-time.

> One option is to simply open and close a dialog box for each selected
item...
> this is not ideal for this application but if I can't figure out how to
modify
> the current Form dynamically then it will have to do.

You can use our library 'FormContainer & Transition effects'. With it you
could insert a form as a child window into another form (and make transition
effects if you want). The general interface of the demo included in the
evaluation version is similar to your description.

You can get extended information and download the evaluation version at
http://www.teemach.com/formcontainer

Regards,
Francisco Sanchez

formcontai...@apdo.com
http://www.teemach.com/formcontainer

Other Threads