Recent

Author Topic: How to dynamically create components on form?  (Read 61838 times)

xxpyton

  • Newbie
  • Posts: 5
Re: How to dynamically create components on form?
« Reply #30 on: December 15, 2012, 09:13:02 pm »
thank you for the help on how to create the combo box.

what i wanted to do was this (but with combobox indead of button):
Eh, TList??

Code: [Select]
var
  Form1: TForm1;
  ButtonList: TList;

implementation

procedure TForm1.FormCreate(Sender: TObject);

var i: integer
   btn: TButton;   
begin
  // List to store buttons
  ButtonList := TList.Create;
  for i := 0 to 9 do
  begin
    btn := TButton.Create(self);
    btn.Parent := self;
    btn.Top := (i * (btn.Height + 3));
    // ....

    // Store the button for later reference
    ButtonList.Add(btn);
  end;

  // Demo: label all buttons
  for i := 0 to ButtonList.Count-1 do
    TButton(ButtonList[i]).Caption := format('Button %d', [i]);

end;

Note: put all variables related to the form in the form class type specification.

For example:
Code: [Select]
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    ButtonList: TList;
  public
    { public declarations }
  end;

the problem is, when i click on my Button btnAddComboBox the new combobox doesn't appear under the first one (maybe they are laying on top of each other or the second one is the same as the first one...this is what i don't understand)

to answer your question, i have some global var:

  cbList: TList; //holds all added comboboxes
  cbText: TComboBox; //template for comboboxes
  slData: TStringList; //items inside combobox

slData has two string 'Hello' and 'World'

i want my program to start with one combox where i can select one of the two strings and a button which shoud create a copy of the first combobox under it!

xxpyton

  • Newbie
  • Posts: 5
Re: How to dynamically create components on form?
« Reply #31 on: December 16, 2012, 03:45:35 pm »
got it to work! i have been thinking 2 hours about what you told me and came to the conclusion how this component stuff might work. i'd like to share my thoughts with you and hopefully get an answer that confirms my conclusion.

i missunderstood the component creation completely. till yesterday i was convinced by declaring var cbText: TComboBox; that cbText holds all the data and is allocated somewhere in memory. but by looking carefully i found out that it is only a pointer, so the data/memory must be created and stored by the component TComboBox, which i can access through the pointer cbText or, in my case, a TList with all pointer to all my Comboboxes i've created with command TComboBox.Create(Self) (which returns only a pointer)!

Is this affermation more or less correct?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to dynamically create components on form?
« Reply #32 on: December 16, 2012, 04:09:50 pm »
Quote
Is this affermation more or less correct?
Yes, if you read the documentation, classes are just pointers to object. So when you declare a variable of type class, it only declares the pointer. You have to instantiate the object with .Create method of the class, which you have done correctly (assuming Self refers to the form holding the comboboxes).

xxpyton

  • Newbie
  • Posts: 5
Re: How to dynamically create components on form?
« Reply #33 on: December 16, 2012, 05:45:07 pm »
thank you.
no i didn't read the documentation (where can i find it?). i ordered this book last week:
http://www.amazon.com/Lazarus-Complete-Guide-pascal-teaching/dp/9490968021/ref=sr_1_4?ie=UTF8&qid=1355674911&sr=8-4&keywords=lazarus+ide
but santa seems to be really busy these days... still waiting for it. by the way, if somebody knows this book, is it a good book for oop-beginners?
till then i will continue guessing and experimenting (there is no risk of killing my operating system by doing dump programs  O:-) or can i  >:D ?)

next think i want to do is getting rid of those comboboxes i just added with, lets say, a btnSubComboBox, can somebody point me to a page/post/tutorial that explains how to correctly free/destroy stuff?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to dynamically create components on form?
« Reply #34 on: December 16, 2012, 05:49:28 pm »
documentation: try the wiki. Link to the left.

You've been missing quite a lot of FPC & Lazarus documentation, tutorials, code snippets etc...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to dynamically create components on form?
« Reply #35 on: December 16, 2012, 05:53:25 pm »
Quote
no i didn't read the documentation (where can i find it?)
http://www.freepascal.org/docs.var

It seems like another missing link between Lazarus and Free Pascal...

xxpyton

  • Newbie
  • Posts: 5
Re: How to dynamically create components on form?
« Reply #36 on: December 16, 2012, 06:00:58 pm »
You've been missing quite a lot of FPC & Lazarus documentation, tutorials, code snippets etc...

what do you mean (is it a question), sorry my english isn't that good... must be almost two decades i got my last english lesson  :-\

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to dynamically create components on form?
« Reply #37 on: December 16, 2012, 06:23:29 pm »
I meant that that wiki has a lot of information like tutorials etc that you have not seen.

As for your english... no problem: it's probably better than my command of your native tongue :)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

alaa123456789

  • Sr. Member
  • ****
  • Posts: 260
  • Try your Best to learn & help others
    • youtube:
Re: How to dynamically create components on form?
« Reply #38 on: May 15, 2022, 07:32:23 pm »
For example:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var btn: TButton;
begin
  btn := TButton.Create(self); 
  btn.parent := self; 
  btn.top := ....
  // etc...
end;
dynamic controls it doesnt work in formcreate event , what the reason??
thanks

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to dynamically create components on form?
« Reply #39 on: May 15, 2022, 09:12:40 pm »
Can you share a compilable example that "doesn't work"?

Dynamic creation of controls works as expected here.

speter

  • Sr. Member
  • ****
  • Posts: 345
Re: How to dynamically create components on form?
« Reply #40 on: May 16, 2022, 02:00:52 am »
Try setting the form at the parent. For example

Code: Pascal  [Select][+][-]
  1.   xx := tbutton.create(self);
  2.   with xx do
  3.     begin
  4.       Top := 100;
  5.       left := 100;
  6.       width := 100;
  7.       height := 30;
  8.       caption := 'foo';
  9.       visible := true;
  10.       parent := form1;
  11.     end;  

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

alaa123456789

  • Sr. Member
  • ****
  • Posts: 260
  • Try your Best to learn & help others
    • youtube:
Re: How to dynamically create components on form?
« Reply #41 on: May 16, 2022, 06:52:02 am »
Can you share a compilable example that "doesn't work"?

Dynamic creation of controls works as expected here.

yes it doesnt work ,please see attachment

thanks

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: How to dynamically create components on form?
« Reply #42 on: May 16, 2022, 07:07:42 am »
Can you share a compilable example that "doesn't work"?

Dynamic creation of controls works as expected here.

yes it doesnt work ,please see attachment

thanks

Try putting the button in the visible area for the form.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

alaa123456789

  • Sr. Member
  • ****
  • Posts: 260
  • Try your Best to learn & help others
    • youtube:
Re: How to dynamically create components on form?
« Reply #43 on: May 16, 2022, 07:11:51 am »

[/quote]

Try putting the button in the visible area for the form.
[/quote]

already it is in the visible area , please check the file which attached previously

thanks

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to dynamically create components on form?
« Reply #44 on: May 16, 2022, 07:15:14 am »
The adapted project attached works as expected for me.

 

TinyPortal © 2005-2018