Solved

dynamic creation of a component within a component

Posted on 2002-07-10
9
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:geobul
ID: 7145538
Also:
OnClick := UpDown.OnClick;
seems to be wrong.
0
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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