Link to home
Start Free TrialLog in
Avatar of jbeh
jbeh

asked on

dynamic creation of a component within a component

I've been trying to place components on a form dynamically and have got as far as the code below successfully.

What I now need to do is create the same as below BUT with a linked TUpDown control.

What I can't work out is

how to create it and more to the point link it up.

I know that I need to create the component somewhere within this procedure but I can't see where to use the TUpDown.associate property.

Note this is just code in a unit - I'm not particularly trying to create a component although that may be the way to go.

TVMIA

John


procedure DynaIBEdit(form: Tform; myparent: Twincontrol; mytop, myleft, mywidth: integer; myds: TIB_datasource; mydf:
  string);
begin
  with TIB_edit.Create(form) do begin
    parent := myparent;
    top := mytop;
    left := myleft;
    width := mywidth;
    DataSource := myds;
    datafield := mydf;
end;
ASKER CERTIFIED SOLUTION
Avatar of DrDelphi
DrDelphi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbeh
jbeh

ASKER

Unfortunately I am not quite getting the effect I am trying to acheive.


If I put the components on a form at design time as follows

    inherited Ts_Edit: TTabSheet
      object IB_Edit1: TIB_Edit
        Left = 584
        Top = 44
        Width = 109
        Height = 21
        DataField = 'probability_of_getting'
        DataSource = IB_DataSource_base
        TabOrder = 1
      end
      object UpDown1: TUpDown
        Left = 693
        Top = 44
        Width = 12
        Height = 21
        Associate = IB_Edit1
        Min = 0
        Increment = 5
        Position = 0
        TabOrder = 2
        Wrap = False
        OnClick = UpDown1Click
      end
    end


I get the functionality I need which amounts to a data aware spinner control (used for forcing integers between 0 and 100) without needing to write any events at all.

What I currently have is

procedure DynaIBspinEdit(form: Tform; myparent: Twincontrol; mytop, myleft, mywidth: integer; myds: TIB_datasource; mydf:
  string);
var
  UpDown                      : TupDown;
  TibEdit                     : Tib_edit;
begin
  TibEdit := TIB_edit.Create(form);
  with TibEdit do begin
    parent := myparent;
    top := mytop;
    left := myleft;
    width := mywidth;
    DataSource := myds;
    datafield := mydf;
  end;
  UpDown := TUpDown.create(form);
  with UpDown do begin
    Parent := tibedit;
    ArrowKeys := true;
    min := 0;
    max := 100;
    increment := 5;
    position := 0;
    OnClick := UpDown.OnClick;
    Associate := tibedit;
  end;
end;

I'm wondering if I've got a parenting issue

  UpDown := TUpDown.create(self); fails - Undeclared identifier self.


Anyway - I am very close but I'm not quite there yet..


John



Hi,
I think that UpDown.Parent should be the same as TibEdit.Parent.

Regards, Geo
Also:
OnClick := UpDown.OnClick;
seems to be wrong.
Avatar of jbeh

ASKER

Geo

Hi - I'll make the changes although I think when I was playing last night I tried this to no avail.

I'll have another go anyway

Ta

John
The following work fine I think (I've used TEdit):

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure DynaIBspinEdit(form: Tform; myparent: Twincontrol; mytop, myleft, mywidth: integer);
    procedure UDClick(Sender: TObject; Button: TUDBtnType);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DynaIBspinEdit(form: Tform; myparent: Twincontrol; mytop, myleft, mywidth: integer);
var
 UpDown                      : TupDown;
 TibEdit                     : Tedit;
begin
 TibEdit := TEdit.Create(form);
 with TibEdit do begin
   parent := myparent;
   top := mytop;
   left := myleft;
   width := mywidth;
//   DataSource := myds;
//   datafield := mydf;
 end;
 UpDown := TUpDown.create(form);
 with UpDown do begin
   Parent := myparent;
   ArrowKeys := true;
   min := 0;
   max := 100;
   increment := 5;
   position := 0;
   OnClick := UDClick;
   Associate := tibedit;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DynaIBspinEdit(Form1, Form1, 50, 50 ,100);
end;

procedure TForm1.UDClick(Sender: TObject; Button: TUDBtnType);
begin
  ShowMessage('UpDown was clicked');
end;

Regards, Geo
Avatar of jbeh

ASKER

Thank you Gents.

I have now got this working satisfactorily with the following code

procedure DynaIBspinEdit(form: Tform; myparent: Twincontrol; mytop, myleft, mywidth: integer; myds: TIB_datasource; mydf:
  string);
var
  UpDown                      : TupDown;
  TibEdit                     : Tib_edit;
begin
  TibEdit := TIB_edit.Create(form);
  with TibEdit do begin
    parent := myparent;
    top := mytop;
    left := myleft;
    width := mywidth;
    autolabel.kind := albleft;
    autolabel.Options := [{albdotleader,} albonlyfirstcharupper, albboldifrequired {, albsuffixbeforedots}];
    autolabel.Suffix := ' :--';
    DataSource := myds;
    datafield := mydf;
 end;
  UpDown := TUpDown.create(form);
  with UpDown do begin
    Parent := myparent;
    ArrowKeys := true;
    min := 0;
    max := 100;
    increment := 5;
    position := 0;
    wrap := true;
    OnClick := OnClick;
    Associate := tibedit;
  end;
end;

I think part of my problem is that I had similar designed control for comparison purposes and things didn't work until I dumped it.  

Truly don't understand why but there you go. :-)


I am NOT 100% sure why this has taken me so long but as I have now got it sorted I can start making progress again.

Thank you both very much.

All other things being equal I'll close this question tomorrow and pay my dues


Avatar of jbeh

ASKER

Thanks all

John
I must say that your choice is not fair and will mislead people who will buy this question to use wrong code.

Regards, Geo