问题
i have this type
type
TMain = class(TForm)
panel1: Tpanel;
panel2: Tpanel;
panel3: Tpanel;
panel4: Tpanel;
panel5: Tpanel;
panel6: Tpanel;
panel7: Tpanel;
panel8: Tpanel;
......
panel45: Tpanel;
label1: TLabel;
label2: TLabel;
label3: TLabel;
label4: TLabel;
label5: TLabel;
label6: TLabel;
label7: TLabel;
...........
label109: TLabel;
How can i call this components in one line... inside the Type?
Thank you ...
UpDate....
Base from the anwser i get and accepted it works great when i have all this components and make the actions like a button1.click from the main form...
But i use to make the actions from units... so
When i click a button i great a procedure DoMaths(Sender: TObject);
procedure Tform1.DoMaths(Sender: TObject);
begin
if TButton1(Sender).hint := 'Make the standard Package' then
do_Maths_standard_package;
end;
the do_Maths_standard_package is in unit ComplexMaths.
is the procedure do_Maths_standard_package form unit ComplexMaths it calls some components form Form1... like Form1.label1 etc...
So when i call the RegisterClass(TLabel) and erase the Tlabel from the type it gives an error that it cant find the Label1...
Please can someone help me so not to do the hole program from the start...
Thank you again...
回答1:
You can delete the name of a TPanel or TLabel then it only exists in the Controls List not in the Type declaration of the form. You either need to leave one Label and one panel or add:
initialization
RegisterClass(TPanel);
RegisterClass(Tlabel);
end.
at the end of the form.
This makes forms with a lot of controls much neater.
回答2:
Use the TForm.Controls
array:
var
i: Integer;
Pnl: TPanel;
begin
for i := 0 to ControlCount - 1 do
if Controls[i] is TPanel then
begin
Pnl := TPanel(Controls[i]);
Pnl.Caption := 'This is panel ' + IntToStr(i);
end;
end;
Delphi automatically creates two lists for each TWinControl
:
Controls
contains a list of allTControl
items the control contains.Components
is a list of all theTComponents
on a control.
Note that all Controls
are Components
, but not all Components
are Controls
; that's why there are two lists. (A TDataSet
, for instance, is in the Components
list, but not in the Controls
list; a TEdit
, on the other hand, will be in both.)
You can use the same technique to iterate through the components on a panel or other container as well - TPanel
has both Control
and Component
arrays, for instance.
If what you actually want is to reduce the number of items inside the type
declaration itself, create them at runtime instead - Delphi will automatically add them to the arrays based on the Owner
and Parent
:
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
Panel: TPanel;
Label: TLabel;
begin
for i := 0 to 10 do
begin
Panel := TPanel.Create(Self); // Set who frees it
Panel.Parent := Self; // Set display surface
Panel.Align := alTop;
Panel.Name := Format('Panel%d', [i]); // Not necessary
Panel.Caption := Panel.Name;
// Add a label on each panel, just for fun.
Label := TLabel.Create(Panel); // Panel will free label
Label.Parent := Panel; // Label will show on panel
Label.Top := 10;
Label.Left := 10;
Label.Name := Format('Label%d', [i]);
Label.Caption := Label.Caption; // Not necessary
end;
end;
Note that creating them yourself is not an "optimization", as it just shifts the loading from the VCL doing it to you doing it yourself. It will reduce the size of the .dfm file, but won't speed up your code or loadtime any, and it means you can't visually lay out the form as well. (It's also much harder to maintain your code, because your controls have meaningless names. It's much easier to know what LastNameEdit
or EditLastName
is than Edit1
when you read the code 6 months from now.)
回答3:
You can use arrays:
panels : array[1..45] of TPanel;
This would allow you to create an array of your controls, and access and use them by index.
来源:https://stackoverflow.com/questions/12081700/how-to-eliminate-variables-panel1-panel2-panel3-etc-in-delphi