?
Solved

dynamic creation of a component within a component

Posted on 2002-07-10
9
Medium Priority
?
286 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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