Board index » delphi » Dynamic controls created at runtime

Dynamic controls created at runtime

Hi Folks:

Can anyone give me a code snippet or brief summary of what I need to do in
order to get UI controls on a form to be dynamically created and destroyed at
runtime?  I have a series of check boxes and radio buttons that I would like to
throw on the screen depending on previous user selections.  Thanks in
advance...

--David
dali...@laser.net

 

Re:Dynamic controls created at runtime


In article <423a35$...@news.laser.net>, dali...@laser.net (David Alison) says:

Quote

>Can anyone give me a code snippet or brief summary of what I need to do in
>order to get UI controls on a form to be dynamically created and destroyed at
>runtime?  I have a series of check boxes and radio buttons that I would like to
>throw on the screen depending on previous user selections.  Thanks in
>advance...

When you find out, let me know, I am trying to get TShapes on the screen, but
don't know where their click/mouse events get handled...

In the "\DELPHI\DEMOS\DYNAINST" sub-directory you should find a basic example,
that's what got me started...

Quote
>--David
>dali...@laser.net

--
Jason J. Kaczor
DarkSpyre Solutions.
FAX/VM:                                 (604) 251-9941
mailto:jkac...@darkspyre.com           mailto:jkac...@direct.ca
mailto:102222....@compuserve.com       CompuServe: 102222,553
Day Contact mailto:Jason_J_Kac...@email.bcse.gov.bc.ca

Re:Dynamic controls created at runtime


On:       31.08.95
wrote:    David Alison  - dali...@laser.net

Quote
> Can anyone give me a code snippet or brief summary of what I need to
> do in order to get UI controls on a form to be dynamically created
> and destroyed at runtime?  I have a series of check boxes and radio
> buttons that I would like to throw on the screen depending on
> previous user selections.  Thanks in advance...

Check \Delphi\Demos\DynaInst :-)

Frank

Re:Dynamic controls created at runtime


In article <424hvs$...@grid.Direct.CA> jkac...@darkspyre.com (Jason J. Kaczor) writes:

Quote
>From: jkac...@darkspyre.com (Jason J. Kaczor)
>Subject: Re: Dynamic controls created at runtime
>Date: 31 Aug 1995 14:46:20 GMT
>In article <423a35$...@news.laser.net>, dali...@laser.net (David Alison) says:

>>Can anyone give me a code snippet or brief summary of what I need to do in
>>order to get UI controls on a form to be dynamically created and destroyed at
>>runtime?  I have a series of check boxes and radio buttons that I would like to
>>throw on the screen depending on previous user selections.  Thanks in
>>advance...

I have a couple ideas. What i often do (quick & dirty) is drop all required
components on the form. Then I have a procedure that toggles the various
'visiblke' properties, depending on whatever conditions you want. just
remember to call this toggler at the appropriate times, and the form changes.
bit of a bind at design time though all these components sitting around.

One good dodge  is if you have a notebook, and a control you want on
multiple pages, then you drop these controls onto the form itself (not the
nbook), then enlarge the notebook to cover them. They show through.  Then you
hook their toggler to the tabset's onclick event, and they come & go as you
change pages ! works quite well and helps with the infamous tabbed notebook
out of resources errors.

hope this helps

boris

Re:Dynamic controls created at runtime


In article <5stOHv7_...@ufo.sax.de>, f...@ufo.sax.de (Frank Heyne) says:

Quote

>On:       31.08.95
>wrote:    David Alison  - dali...@laser.net

>> Can anyone give me a code snippet or brief summary of what I need to
>> do in order to get UI controls on a form to be dynamically created
>> and destroyed at runtime?  I have a series of check boxes and radio
>> buttons that I would like to throw on the screen depending on
>> previous user selections.  Thanks in advance...

>Check \Delphi\Demos\DynaInst :-)

Ahhh... but it let's you create them, not use them... look at this code...

procedure TMDI_Child.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
   Instance: TShape;
begin
   If Sender Is TForm then
      Begin
           Instance := TShape.Create(Self);
           Shape_Count := Shape_Count + 1;
           With Instance Do
                begin
                     Parent := Self;
                     Left := X - 2;
                     Top := Y - 2;
                     Width := 4;
                     Height := 4;
                     Tag := Shape_Count;
                     Shape := stCircle;
                     Brush.Color := clMaroon;
                     Brush.Style := bsFDiagonal;
                     Canvas.Pen.Width := 40;
                     OnMouseDown := MyMouseDown;
                     Visible := True
                end;
           Application.ProcessMessages;
           label1.Caption := 'Mk_' + IntToStr(Instance.Tag);
      end;
end;

procedure TMDI_Child.MyMouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Integer);
var
   TagInfo: Integer;
begin
     If Sender Is TShape then
        begin
             With Sender Do
                  begin
                       TagInfo := Tag;
                       label1.Caption := 'Ck_' + IntToStr(TagInfo);
                  end;
        end;
end;

I finally get the mouse messages for each "circle" shape my user clicks
onto the form... BUT, there is not info associated with the Sender object
in the  TMDI_Child.MyMouseDown procedure.  It gets fired when a "circle"
is clicked, but the is not info....  I need at least the Top, and Left
properties... ugh... can anyone help...?

perhaps I have to declare "Instance" at the unit level, or maybe,
I have to add it to a TList/Collection ?... I dunno, I am lost at
this point..

Quote
>Frank

--
Jason J. Kaczor
DarkSpyre Solutions.
FAX/VM:                                 (604) 251-9941
mailto:jkac...@darkspyre.com           mailto:jkac...@direct.ca
mailto:102222....@compuserve.com       CompuServe: 102222,553
Day Contact mailto:Jason_J_Kac...@email.bcse.gov.bc.ca

Re:Dynamic controls created at runtime


Jason J. Kaczor (jkac...@darkspyre.com) wrote:
: procedure TMDI_Child.FormMouseDown(Sender: TObject; Button: TMouseButton;
:   Shift: TShiftState; X, Y: Integer);
: var
:    Instance: TShape;

Note above, that Instance is declared as a TShape.

: procedure TMDI_Child.MyMouseDown(Sender: TObject; Button: TMouseButton;
:    Shift: TShiftState; X, Y: Integer);
: var
:    TagInfo: Integer;
: begin
:      If Sender Is TShape then
:         begin
:              With Sender Do
:                   begin
:                        TagInfo := Tag;
:                        label1.Caption := 'Ck_' + IntToStr(TagInfo);
:                   end;
:         end;
: end;
:
: perhaps I have to declare "Instance" at the unit level, or maybe,
: I have to add it to a TList/Collection ?... I dunno, I am lost at
: this point..

Note above, that Sender is declared as a TObject, which is a base
class of TShape (well, it's a base class of everything :).  TObject
only has the bare methods common to ALL objects.  "If Sender is
TShape" merely returns TRUE if the Sender object was declared a
TShape or higher, but it does NOT unmask the higher-level functions
TShape has.  To do this, use:

  if Sender is TShape then
    with Sender as TShape do  { Uses it as a TShape object }
    begin
      {...}
    end;

You can actually also go:

  with Sender as TShape do
  begin
    {...}
  end;

by itself, but if Sender is not a TShape, this throws a silent
exception. (I haven't played with that enough yet to find out how
that exception gets handled - it might be worth it to try it to
see if it will save you the IF, because non-TShapes are guaranteed
not to get in that WITH)

If you just wanted to do something with components (which have the
Top and Left), of course, TShape also happens to be a component,
so this will work, too:

  if Sender is TComponent then
    with Sender as TComponent do
    begin
      Top := Top+5;
    end;

Or, if you feel silly, you can treat the sender as simply a
component after you ascertain it's a TShape:

  if Sender is TShape then
    with Sender as TComponent do
    begin
      Left := Left+30;
    end;

It's a common bit of confusion, tho'.  This is the third time I've
seen it mentioned this week alone :)

    --=- Ritchie Annand
    New Product Development
    Malibu Engineering Ltd.

Other Threads