Solved

dynamic creation of a component within a component

Posted on 2002-07-10
9
272 Views
Last Modified: 2010-04-04
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;
0
Comment
Question by:jbeh
  • 4
  • 4
9 Comments
 
LVL 6

Accepted Solution

by:
DrDelphi earned 100 total points
ID: 7144450
procedure DynaIBEdit(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(self);
   with UpDown do
   begin
     Parent:=TibEdit;
     Left:=TibEdit.width-10;///default to 10 pixels wide;
     top:=0;
     height:=TibEdit.height;
     OnClick:=ClickEventHandler; /// this is a routine you design to handle
                                 // the click event
   end;
 end;

0
 

Author Comment

by:jbeh
ID: 7145304
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



0
 
LVL 17

Expert Comment

by:geobul
ID: 7145526
Hi,
I think that UpDown.Parent should be the same as TibEdit.Parent.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 7145538
Also:
OnClick := UpDown.OnClick;
seems to be wrong.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jbeh
ID: 7145695
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
0
 
LVL 17

Expert Comment

by:geobul
ID: 7145727
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
0
 

Author Comment

by:jbeh
ID: 7152121
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


0
 

Author Comment

by:jbeh
ID: 7153721
Thanks all

John
0
 
LVL 17

Expert Comment

by:geobul
ID: 7153740
I must say that your choice is not fair and will mislead people who will buy this question to use wrong code.

Regards, Geo
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now