29.07.2022, 21:54
|
Прохожий
|
|
Регистрация: 16.07.2017
Сообщения: 23
Версия Delphi: Delphi XE3
Репутация: 10
|
|
Цитата:
Сообщение от lmikle
Да, просто сделать Assign не получится. Для начала, даже если бы это сработало бы, то скопировались бы свойства только самого GroupBox, а компоненты в нем все равно бы не создались. Т.е. придется писать код самому. Как вариант - сделать фрейм и создавать нужное кол-во таких фреймов.
Второй вариант - написать код для клонирования.
В твоем случае код будет выглядеть так:
Код:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Edit1: TEdit;
btClone: TButton;
GroupBox2: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Edit2: TEdit;
GroupBox3: TGroupBox;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
Edit3: TEdit;
procedure btCloneClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function CloneGroupBox(ASource : TGroupBox; ASourceParent : Boolean; AParent : TGroupBox; X, Y : Integer) : TGroupBox; overload;
function CloneGroupBox(ASource : TGroupBox; ASourceParent : Boolean; AParent : TGroupBox) : TGroupBox; overload;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.CloneGroupBox(ASource : TGroupBox; ASourceParent : Boolean; AParent : TGroupBox; X, Y : Integer) : TGroupBox;
var
I : Integer;
NLabel : TLabel;
NEdit : TEdit;
NRButton : TRadioButton;
begin
Result := TGroupBox.Create(Self);
Result.Left := X;
Result.Top := Y;
Result.Width := ASource.Width;
Result.Height := ASource.Height;
For I := 0 To ASource.ControlCount-1 Do
Begin
if ASource.Controls[i] Is TLabel then
begin
NLabel := TLabel.Create(Self);
NLabel.Left := (ASource.Controls[i] As TLabel).Left;
NLabel.Top := (ASource.Controls[i] As TLabel).Top;
NLabel.Width := (ASource.Controls[i] As TLabel).Width;
NLabel.Caption := (ASource.Controls[i] As TLabel).Caption;
NLabel.Parent := Result;
end;
if ASource.Controls[i] Is TEdit then
begin
NEdit := TEdit.Create(Self);
NEdit.Left := (ASource.Controls[i] As TEdit).Left;
NEdit.Top := (ASource.Controls[i] As TEdit).Top;
NEdit.Width := (ASource.Controls[i] As TEdit).Width;
NEdit.Text := (ASource.Controls[i] As TEdit).Text;
NEdit.Parent := Result;
end;
if ASource.Controls[i] Is TRadioButton then
begin
NRButton := TRadioButton.Create(Self);
NRButton.Left := (ASource.Controls[i] As TRadioButton).Left;
NRButton.Top := (ASource.Controls[i] As TRadioButton).Top;
NRButton.Width := (ASource.Controls[i] As TRadioButton).Width;
NRButton.Caption := (ASource.Controls[i] As TRadioButton).Caption;
NRButton.Parent := Result;
end;
if ASource.Controls[i] Is TGroupBox then
begin
CloneGroupBox(ASource.Controls[i] As TGroupBox,False,Result);
end;
End;
if ASourceParent
then Result.Parent := ASource.Parent
else Result.Parent := AParent;
end;
function TForm1.CloneGroupBox(ASource : TGroupBox; ASourceParent : Boolean; AParent : TGroupBox) : TGroupBox;
begin
Result := CloneGroupBox(ASource,ASourceParent,AParent,ASource.Left,ASource.Top);
end;
procedure TForm1.btCloneClick(Sender: TObject);
begin
CloneGroupBox(GroupBox1,True,Nil,GroupBox1.Left,GroupBox1.Top + GroupBox1.Height + 8);
end;
end.
Написал код только для копирования основных свойств. Если надо, то добавишь копирование нужных, в т.ч. и назначения событий (для этого события придется переписать так, что бы они находили соотв. копоненты сами, а не адресовались напрямую, т.к. при клонировании создаются соответствующие новые контролы).
Ну и когда будешь собирать данные с контролов, то тоже придется идти через Components[i].
|
То есть, в любом случае придётся указывать свойства каждого элемента для клонирования? Быстрых методов дублирования компонентов на форме нет?...
|