gpt4 book ai didi

delphi - 如何复制组件/控件?

转载 作者:行者123 更新时间:2023-12-03 15:41:10 26 4
gpt4 key购买 nike

Possible Duplicate:
Duplicating components at Run-Time

我有一个TMyControl ( Control1 ) 具有自己的属性/事件。
如何创建重复实例 Control2将具有完全相同相同的属性/事件?

<小时/>

更具体地说,我想克隆现有的 TADODataSet带有流字段(和一些事件):

object ADODataSet1: TADODataSet
Connection = ADOConnection1
CursorType = ctStatic
AfterOpen = ADODataSet1AfterOpen
CommandText = 'select top 10 * from Polls'
Parameters = <>
Left = 224
Top = 40
object ADODataSet1PollID: TGuidField
FieldName = 'PollID'
FixedChar = True
Size = 38
end
object ADODataSet1Title: TWideStringField
FieldName = 'Title'
Size = 255
end
object ADODataSet1Description: TWideStringField
FieldName = 'Description'
Size = 4000
end
object ADODataSet1PollType: TIntegerField
FieldName = 'PollType'
end
end
<小时/>

既然您关闭了此问题,如果我提出一个新问题“如何使用持久字段复制TADODataSet,您会考虑重复吗?

最佳答案

以下代码可能会提供一些指导:

unit Unit130;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls;

type
TForm130 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
edit2: TEdit;
public
end;

var
Form130: TForm130;

implementation

{$R *.dfm}

procedure TForm130.Button1Click(Sender: TObject);
var
component: TComponent;
stream: TMemoryStream;
begin
RegisterClass(TEdit);
stream := TMemoryStream.Create;
try
stream.WriteComponent(edit1);
stream.Position := 0;
component := stream.ReadComponent(nil);
edit2 := component as TEdit;

{ this is necessary to make the following InsertComponent work }
edit2.Name := 'Edit2';
InsertComponent(edit2);

edit2.Parent := Self;
edit2.Top := edit2.Top + 30;
finally
stream.Free;
end;
end;

end.

关于delphi - 如何复制组件/控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9468789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com