Pascal 表格1中的Lazarus Listview,可通过表格2添加到

Pascal 表格1中的Lazarus Listview,可通过表格2添加到,pascal,lazarus,Pascal,Lazarus,很抱歉,我有另一个类似的问题,但我希望找到一个更简单的解决办法,我的问题 我希望form2能够添加到form1中的listview。Form2由form1通过按下按钮创建。Form2有一个listview,当我单击Form2中的按钮时,我希望它将所有项目添加到form1中的listview并关闭Form2。最简单的方法是什么 *我尝试使用windows消息传递,但由于某些原因,Lazarus无法使用TWM_CopyData。由于我更喜欢单元引用,以下是可能对您有所帮助的内容。在Form2中声明了

很抱歉,我有另一个类似的问题,但我希望找到一个更简单的解决办法,我的问题

我希望form2能够添加到form1中的listview。Form2由form1通过按下按钮创建。Form2有一个listview,当我单击Form2中的按钮时,我希望它将所有项目添加到form1中的listview并关闭Form2。最简单的方法是什么


*我尝试使用windows消息传递,但由于某些原因,Lazarus无法使用TWM_CopyData。

由于我更喜欢单元引用,以下是可能对您有所帮助的内容。在
Form2
中声明了公共属性
TargetListView
,在显示
Form2
之前,将
Form1
中的列表视图分配到该属性中。现在,您可以从
Form2
范围访问
Form1
列表视图,并且可以在关闭它之前复制其中的项目

以下是第一台机组的简化代码:

unit Unit1;

uses
  Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(Self);
  Form2.TargetListView := ListView1;
  Form2.Show;
end;
下面是第二个单元的简化代码:

unit Unit2;

type
  TForm2 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    TargetListView: TListView;
  end;

procedure TForm2.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  TargetListView.Items.BeginUpdate;
  try
    for I := 0 to ListView1.Items.Count - 1 do
      TargetListView.Items.Add.Assign(ListView1.Items[I]);
  finally
    TargetListView.Items.EndUpdate;
  end;
  Close;
end; 

由于我更喜欢单元引用,以下是可能对您有所帮助的内容。在
Form2
中声明了公共属性
TargetListView
,在显示
Form2
之前,将
Form1
中的列表视图分配到该属性中。现在,您可以从
Form2
范围访问
Form1
列表视图,并且可以在关闭它之前复制其中的项目

以下是第一台机组的简化代码:

unit Unit1;

uses
  Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(Self);
  Form2.TargetListView := ListView1;
  Form2.Show;
end;
下面是第二个单元的简化代码:

unit Unit2;

type
  TForm2 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    TargetListView: TListView;
  end;

procedure TForm2.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  TargetListView.Items.BeginUpdate;
  try
    for I := 0 to ListView1.Items.Count - 1 do
      TargetListView.Items.Add.Assign(ListView1.Items[I]);
  finally
    TargetListView.Items.EndUpdate;
  end;
  Close;
end; 

现在看代码张贴;它们几乎是一样的。我只使用了公共属性
TargetListView
,而不是带有
SetListView
setter函数的私有字段
FListView
,但思想是一样的。所有这些可能也适用于Delphi。忘了解释David在他的帖子中的注释,即您将依赖于主窗体的实现,这也适用于此代码,这意味着什么。这意味着,如果您销毁
表单1
(在我的情况下)上的列表视图,然后单击
表单2
上的按钮,您将获得访问冲突错误,因为您尝试复制项目的列表视图不再存在。但我想你不会这么做的。如果是这样,请留下评论或更新您的问题。不客气!无论如何,如果答案解决了你的问题,不要忘记接受答案()。如果您还有任何与您的问题相关的问题,请在此处留言;-)现在看代码张贴;它们几乎是一样的。我只使用了公共属性
TargetListView
,而不是带有
SetListView
setter函数的私有字段
FListView
,但思想是一样的。所有这些可能也适用于Delphi。忘了解释David在他的帖子中的注释,即您将依赖于主窗体的实现,这也适用于此代码,这意味着什么。这意味着,如果您销毁
表单1
(在我的情况下)上的列表视图,然后单击
表单2
上的按钮,您将获得访问冲突错误,因为您尝试复制项目的列表视图不再存在。但我想你不会这么做的。如果是这样,请留下评论或更新您的问题。不客气!无论如何,如果答案解决了你的问题,不要忘记接受答案()。如果您还有任何与您的问题相关的问题,请在此处留言;-)准确的副本。准确的副本。