The Delphi version used for development is XE2, as follows:
The code is as follows:
--------------------------------------------------------------------------------------------------------------- --
Unit unit1;interfaceuses winapi.windows, Winapi.messages, System.sysutils, system.variants, System.Classes, Vcl.graphics, Vcl.controls, Vcl.forms, Vcl.dialogs, vcl.stdctrls, Vcl.extctrls;type TForm1 = Class (Tform) panel1:tpa Nel Button1:tbutton; Edit1:tedit; Memo1:tmemo; Button2:tbutton; Procedure Button1Click (Sender:tobject); Procedure Button2click (Sender:tobject); Private {Private declarations} function Getmaxval (Myarray:array of Integer): integer; Public {public declarations} End;var form1:tform1;implementation{$R *.dfm}var rec:integer = 0;procedure tform1.b Utton1click (Sender:tobject); var I,j,m,n,p:integer; Edt:tedit; Arr:array of Integer;begin INC (REC); Number of button clicks m: = 0; N: = 0; P: = 0; Memo1.clear; For I: = 0 to Panel1.controlcount-1 does begin if panel1.controls[i] is Tedit then Inc (M); Record the number of edit end; SetLength (ARR,M); Memo1.Lines.Add (' record before component creation: ' + #13 # # + '--------------------------'); MEMO1.LINES.ADD ('Number of Edit components: ' + INTTOSTR (m) + #13 # # + '--------------------------'); For J: = 0 to Panel1.controlcount-1 does begin if PANEL1.CONTROLS[J] is Tedit then BEGIN if n <= m then Begin Arr[n]: = Tedit (Panel1.controls[j]). Top; MEMO1.LINES.ADD (' Edit ' + inttostr (n + 1) + ' top value: ' + inttostr (Arr[n])); INC (n); End else break; End End P: = Getmaxval (arr); Record the top value of the bottom edit in panel Memo1.Lines.Add (' Maximum top value: ' + INTTOSTR (p)); Dynamically create edit//tedit.create (), the parameter is a aowner:tcomponent, as long as the component on the form can,//But you must re-specify the name of the parent component after creation, or you will not be able to display EDT: = Tedit.create ( PANEL1); Specifies that owner can not free memory, and the parent component destroys EDT when the window is closed. Parent: = Panel1; EDT. Left: = Edit1.left; EDT. Top: = p + edit1.height + 3; EDT. Width: = Edit1.width; EDT. Height: = Edit1.height; EDT. Text: = ' Edit ' + inttostr (rec + 1); EDT. Show;end;procedure Tform1.button2click (Sender:tobject); Frees up memory and initializes var I,j,m:integer; Edts:array of tedit;begin J: = 0; The key of the array Edts [key] SetLength (Edts,rec + 1); Number of Button1 + original Edit1 For I: = 0 to Panel1.controlcount-1 does begin if panel1.controls[i] is Tedit then BEGIN EDTS[J]: = Tedit (Pa Nel1. Controls[i]); INC (j); End End For M: = Length (Edts) Downto 1 does//edit1 do not release begin try Edts[m]. Free; Rec: = 0; Memo1.clear; Except//showmessage (' release the memory of dynamically created component failed! ‘); End End;end;function tform1.getmaxval (Myarray:array of Integer): integer; Traversing an array to get the maximum var i,max:integer;begin max: = myarray[0]; For I: = 1 to Length (myarray)-1 does begin if myarray[i] >= max then max: = Myarray[i]; End Result: = Max;end;end.
Delphi dynamically creates components and frees up memory