Pascal将listview从主窗体传递到非模式窗体 [英] Pascal passing a listview from mainform to a nonmodal form

查看:102
本文介绍了Pascal将listview从主窗体传递到非模式窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主窗体中有一个列表视图,我希望另一个非模式窗体可以向其中添加内容. 如何将listview传递给非模式形式? 我想要能够修改的表单通过单击MainForm随即显示,仅使用Form.Show.

I have a listview in my mainform that I want another nonmodal form to be able to add things to. How would I pass the listview to the nonmodal form? The form I want to be able to modify it is shown by the MainForm on a button click, just with Form.Show.

我还应该澄清一下,我希望表单不使用主表单来避免循环引用.

I should also clarify that I want the form to not use the mainform to avoid circular reference.

推荐答案

注意:该问题最初被标记为Delphi.我不知道FPC/Lazarus,因此下面的某些细节可能不准确,但这些概念仍然有效.

Note: The question was originally tagged Delphi. I don't know FPC/Lazarus so some of the details may not be accurate below but the concepts are still valid.

我猜您有一个代表主要形式的全局变量,为便于讨论,命名为MainForm.最快,最简单的方法就是简单地使用MainForm.ListView来让您的其他表单引用主表单的列表视图.

I'm guessing that you have a global variable representing the main form, named MainForm for the sake of argument. The quickest and simplest approach is simply to use MainForm.ListView to let your other form refer to the main form's list view.

我并不十分喜欢这种方法,因为这意味着另一种形式依赖于主形式的实现.

I'm not terribly keen on this approach since it means that the other form takes a dependency on the implementation of the main form.

作为替代方案,您可以将对列表视图的引用传递给其他表单.例如,您可以将SetListView过程添加到其他表单中:

As an alternative, you can pass a reference to the list view to the other form. For example you could add a SetListView procedure to your other form:

type
  TMyOtherForm = class(TForm)
  private
    FListView: TListView;
  public
    procedure SetListView(Value: TListView);
  end;
.....
procedure TMyOtherForm.SetListView(Value: TListView);
begin
  FListView := Value;
end;

然后在主窗体中,可能在主窗体的OnCreate事件处理程序中,可以调用此方法:

Then in the main form, probably in the OnCreate event handler for the main form, you can call this method:

procedure TMainForm.FormCreate(Sender: TObject);
begin
  MyOtherForm.SetListView(ListView);
end;

这实际上并没有比第一个建议好得多.另一种形式仍然依赖于主形式的实现,尽管这种依赖程度可能不那么重要.

This isn't really all that much better than the first suggestion. The other form still takes a dependency on the implementation of the main form, albeit an arguably less significant dependency.

如果您想使两种形式的耦合程度更低,则主形式可以公开一种接受添加到列表的方法:

If you want to keep the two forms even less coupled then the main form could expose a method that accepts additions to the list:

procedure TMainForm.AddToList(const Name: sting; const Time: TDateTime);
var
  Item: TListItem;
begin
  Item := ListView.Items.Add;
  Item.Caption := Name;
  Item.SubItems.Add(DateTimeToStr(Time));
end;

在其他表单单元中,您可以将主表单单元添加到实现部分的uses子句中.这样您就可以访问MainForm全局变量,并且可以调用

The in your other forms unit you add the main form unit to the uses clause in the implementation section. That allows you access to the MainForm global variable and you can call

MainForm.AddToList(Name, Time);

在我看来,这种方法是最好的,因为它允许主窗体将其列表实现对自己保密.

This approach is the best in my view because it allows the main form to keep its list implementation private to itself.

您在注释中声明,您希望避免由于uses子句而产生任何循环引用.这很容易安排,因为上述所有内容均不需要修改所涉及的两个单元的接口部分中的uses子句.

You state in a comment that you want to avoid any circular references due to uses clauses. That's easy to arrange since none of the above require modifications to the uses clause from the interface section from the two units in question.

这篇关于Pascal将listview从主窗体传递到非模式窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆