Re: Creating objects on a form at runtime

Posted by DelphiLand Team on February 24, 2005

In Reply to: Creating objects on a form at runtime posted by Al p12406 on February 23, 2005

: ...I need a way of creating an unspecified number of entry fields on a Delphi form. Can anyone give an example of how to do this? My other thought is to create an upper limit (i.e. just create 100, say, fields and then disable/enable as required) - but I'd rather my utility was totally scalable.

Of course, you can create a number of Edits dynamically. But there are disadvantages to this:

1. If you create a lot of TEdit components, this can become to heavy on the resources of Windows. And eventually, you will reach the system's limits. The limits depend on the number of "windowed controls" that are created by your program as well as all other running programs. This includes not only "windows" (forms) but also components like Edit, ListBox, and so on.
These limits also depend on the version of the operating system (XP, 98, 95,...).

2. Creating lots of components at runtime takes time, which can be annoying on older PC's.

3. Last but not least, it's quite complicated to keep track of the newly created components. Because you don't know how much there will be, you have to create a list and add pointers to this list as you create the Edits. Later on, it's also not so easy to access the contents of the Edits.
Finally, at the end of the program, you have to "free" (destroy) the components and free the list.

But it's a lot easier to use a TStringGrid component. You can:

- dynamically change its number of columns and rows:
StringGrid1.ColCount := 2;
StringGrid1.RowCount := 100;
- access the individual cells easily:
S := StringGrid1[2,1];
puts the text of the third column / second row in variable S (numbering starts at 0).

Which one do you prefer: do you insist on Edits, or do you want the StringGrid solution? Just let us know, and we'll cook up some well-tested code for you :))

Related Articles and Replies


[ Delphi Tutorials -- by DelphiLand ]