windows delphi中的多种形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2471415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:08:55  来源:igfitidea点击:

Multiple forms in delphi

windowsdelphiformsdelphi-7

提问by chendriksen

In my Delphi Project i want to have a 'Settings' button that when clicked, opens a second form (i think this is the correct term, i essentially want a new window to open) for settings.

在我的 Delphi 项目中,我想要一个“设置”按钮,单击该按钮时,会打开第二个表单(我认为这是正确的术语,我本质上希望打开一个新窗口)进行设置。

When the user has finished changing the settings on this new form, i want the form to close on a button click.

当用户完成更改此新表单上的设置后,我希望该表单在单击按钮时关闭。

The settings the user types in will also need to be accessible to the first, 'main' form.

用户输入的设置也需要可以被第一个“主”表单访问。

So, for example, if my programme consisted of a main form, that calculated 'A' + 'B' (A and B being integer variables), with the settings form allowing the user to set values for A and B, how would i do this?

因此,例如,如果我的程序由一个主窗体组成,它计算了 'A' + 'B'(A 和 B 是整数变量),设置窗体允许用户设置 A 和 B 的值,我将如何做这个?

回答by Mason Wheeler

That's not too difficult if you understand Delphi's object model. A form is an object that descends from TObject, backed by a DFM file to make setting up the layout easier. The controls on it are other objects, and by default they're publicly visible to other objects from other units, like your other form. There are two ways to do this.

如果您了解 Delphi 的对象模型,这并不难。表单是继承自 TObject 的对象,由 DFM 文件支持,以便更轻松地设置布局。它上的控件是其他对象,默认情况下,它们对来自其他单元的其他对象公开可见,就像您的其他表单一样。有两种方法可以做到这一点。

The easy way is to have your other form's code read the values from the controls directly once you're done with the first form. Just use stuff like MyString := Form2.EditBox.Text;. This isn't particularly good style, but it works.

简单的方法是在您完成第一个表单后,让其他表单的代码直接从控件中读取值。只需使用诸如MyString := Form2.EditBox.Text;. 这不是特别好的风格,但它有效。

The right way to do it is to either put public properties on your form that will retrieve the values of the controls, or a function that will read them and return some sort of object or record containing all the settings. This takes a bit more work, but it results in cleaner code that's less likely to cause trouble if you modify things down the road.

正确的做法是将公共属性放在表单上以检索控件的值,或者将读取它们并返回某种对象或包含所有设置的记录的函数。这需要做更多的工作,但它会产生更干净的代码,如果您在路上修改内容,则不太可能引起麻烦。

EDIT: In response to the question in the comment:

编辑:回应评论中的问题:

To make one form show and hide another, you call Show and Hide on it. Or if you want it to show up in a modal dialog box, call the ShowModal method, which takes care of the hiding for you, as long as you create a button that sets ModalResult. (See the helpfile for details on how these methods work.)

要使一种形式显示并隐藏另一种形式,请在其上调用 Show 和 Hide。或者,如果您希望它显示在模式对话框中,请调用 ShowModal 方法,该方法会为您处理隐藏操作,只要您创建一个设置 ModalResult 的按钮即可。(有关这些方法如何工作的详细信息,请参阅帮助文件。)

Of course, the form has to have been created first. Either it can be autocreated by the DPR, which is good for simple programs but not so great once you get a lot of forms in your app, or you can create it in code. Henk has an example of how to do that, though I wouldn't recommend using the withkeyword. And if you created the form yourself, make sure to Free it afterwards.

当然,必须先创建表单。要么它可以由 DPR 自动创建,这对简单的程序很有用,但一旦你的应用程序中有很多表单就不是那么好,或者你可以在代码中创建它。Henk 有一个如何做到这一点的例子,但我不建议使用with关键字。如果您自己创建了表单,请确保之后将其释放。

回答by dummzeuch

I usually design the settings form and add a class function execute to it, that changes a record containing fields for the settings like this:

我通常设计设置表单并向其添加一个类函数执行,它更改包含如下设置字段的记录:

Tf_MySettings = class(TForm)
  // ...
private
  procedure SetData(const _Settings: TSettingsRec);
  procedure GetData(out _Settings: TSettingsRec);
public
  class function Execute(_Owner: TComponent; var _Settings: TSettingsRec): boolean;
end;

implementation

function Tf_MySettings.Execute(_Owner: TComponent; var _Settings: TSettingsRec): boolean;
var
  frm: Tf_MySettings;
begin
  frm := Tf_MySettings.Create(_Owner);
  try
    frm.SetData(_Settings);
    // for this to work, the OK button must have ModalResult=mrOK
    Result := frm.ShowModal = mrOK;
    if Result then
      frm.GetData(_Settings);
  finally
    frm.Free;
  end;
end;

procedure Tf_MySettings.SetData(const _Settings: TMySettingsRec);
begin
  ed_Name.Text := _Settings.Name;
  // ...
end;

procedure Tf_MySettings.GetData(out _Settings: TMySettingsRec);
begin
  _Settings.Name := ed_Name.Text;
  // ...
end;

You call it like this:

你这样称呼它:

if Tf_MySettings.Execute(self, _Settings) then begin
  // settings have been changed
end;

回答by Daniel Luyo

I would create a Settings Objects in form1 and send it to form 2:

我会在 form1 中创建一个 Settings Objects 并将其发送到 form 2:

TSettings = class 
private    
  FPropA: integer;   
  FPropB: string; 
published 
  property PropA: integer read FPropA write FPropA:  
  property PropB: string read FPropB write FPropB: 
end;

...
form2.EditSettings(ASettings);