表单之间值传递_表单之间的沟通

表单之间值传递

Modal forms offer specific features that we cannot have when displaying non-modally. Most commonly, we will display a form modally to isolate its processes from anything that might otherwise happen on the main form. Once these processes complete, you might want to know whether the user pressed the Save or Cancel button to close the modal form. You can write some interesting code to accomplish this, but it does not have to be difficult. Delphi supplies modal forms with the ModalResult property, which we can read to tell how the user exited the form.

模态表单提供了非模态显示时无法提供的特定功能。 最常见的是,我们将以模态形式显示表单,以将其过程与主表单上可能发生的任何事件隔离开。 这些过程完成后,您可能想知道用户是按“保存”还是“取消”按钮关闭模式窗体。 您可以编写一些有趣的代码来完成此操作,但这并不困难。 Delphi为模态表单提供了ModalResult属性,我们可以阅读该属性以了解用户如何退出表单。

The following code returns a result, but the calling routine ignores it:

以下代码返回结果,但是调用例程将其忽略:


var
F:TForm2;
begin
F := TForm2.Create(nil);
F.ShowModal;
F.Release;
...

The example shown above just shows the form, lets the user do something with it, then releases it. To check how the form was terminated we need to take advantage of the fact that the ShowModal method is a function that returns one of several ModalResult values. Change the line

上面显示的示例仅显示了表单,让用户对其进行操作,然后释放它。 要检查表单是如何终止的,我们需要利用ShowModal方法是返回几个ModalResult值之一的函数这一事实。 换线

F.ShowModal

显示模态

to

if F.ShowModal = mrOk then

如果 F.ShowModal = mrOk,

We need some code in the modal form to set up whatever it is we want to retrieve. There is more than one way to get the ModalResult because TForm is not the only component having a ModalResult property - TButton has one too.

我们需要模态形式的一些代码来设置要检索的内容。 获得ModalResult的方法不止一种,因为TForm并不是唯一具有ModalResult属性的组件-TButton也有一种。

Let us look at TButton's ModalResult first. Start a new project, and add one additional form (Delphi IDE Main menu: File -> New -> Form). This new form will have a 'Form2' name. Next add a TButton (Name: 'Button1') to the main form (Form1), double-click the new button and enter the following code:

让我们首先看一下TButton的ModalResult。 开始一个新项目,并添加一个附加表单(Delphi IDE主菜单:文件->新建->表单)。 此新表单将具有“ Form2”名称。 接下来,在主窗体(Form1)中添加一个TButton(名称:'Button1'),双击新按钮并输入以下代码:


procedure TForm1.Button1Click(Sender: TObject);
var f : TForm2;
begin
f := TForm2.Create(nil);
try
if f.ShowModal = mrOk then
Caption := 'Yes'
else
Caption := 'No';
finally
f.Release;
end;
end;

Now select the additional form. Give it two TButtons, labelling one 'Save' (Name : 'btnSave'; Caption: 'Save') and the other 'Cancel' (Name : 'btnCancel'; Caption: 'Cancel'). Select the Save button and press F4 to bring up the Object Inspector, scroll up/down until you find the property ModalResult and set it to mrOk. Go back to the form and select the Cancel button, press F4, select the property ModalResult, and set it to mrCancel.

现在选择其他形式。 给它两个TButton,分别标记一个“保存”(名称:“ btnSave”;标题:“保存”)和另一个“取消”(名称:“ btnCancel”;标题:“取消”)。 选择“保存”按钮,然后按F4键打开“对象检查器”,向上/向下滚动,直到找到属性ModalResult并将其设置为mrOk。 返回到窗体并选择“取消”按钮,按F4,选择属性ModalResult,并将其设置为mrCancel。

It's as simple as that. Now press F9 to run the project. (Depending on your environment settings, Delphi may prompt to save the files.) Once the main form appears, press the Button1 you added earlier, to show the child form. When the child form appears, press the Save button and the form closes, once back to the main form note that it's caption says "Yes". Press the main form's button to bring up the child form again but this time press the Cancel button (or the System menu Close item or the [x] button in the caption area). The main form's caption will read "No".

就这么简单。 现在按F9键运行该项目。 (根据您的环境设置,Delphi可能会提示您保存文件。)一旦出现主窗体,请按之前添加的Button1,以显示子窗体。 当子窗体出现时,按“保存”按钮,窗体关闭,回到主窗体后,其标题显示为“是”。 按主窗体的按钮再次调出子窗体,但这次按“取消”按钮(或“标题”区域中的“系统”菜单的“关闭”项目或[x]按钮)。 主窗体的标题将显示为“否”。

How does this work? To find out take a look at the Click event for TButton (from StdCtrls.pas):

这是如何运作的? 要找出答案,请看一下TButton的Click事件(来自StdCtrls.pas):


procedure TButton.Click;
var Form: TCustomForm;
begin
Form := GetParentForm(Self);
if Form nil then
Form.ModalResult := ModalResult;
inherited Click;
end;

What happens is that the Owner (in this case the secondary form) of TButton gets its ModalResult set according to the value of the TButton's ModalResult. If you don't set TButton.ModalResult, then the value is mrNone (by default). Even if the TButton is placed on another control the parent form is still used to set its result. The last line then invokes the Click event inherited from its ancestor class.

发生的是,TButton的所有者 (在这种情况下为辅助形式)根据TButton的ModalResult的值获取其ModalResult集。 如果未设置TButton.ModalResult,则该值为mrNone(默认情况下)。 即使将TButton放在另一个控件上,父窗体仍将用于设置其结果。 然后,最后一行调用从其祖先类继承的Click事件。

To understand what goes on with the Forms ModalResult it is worthwhile reviewing the code in Forms.pas, which you should be able to find in ..\DelphiN\Source (where N represents the version number).

要了解Forms ModalResult发生了什么,值得回顾Forms.pas中的代码,您应该可以在.. \ DelphiN \ Source中找到该代码(其中N表示版本号)。

In TForm's ShowModal function, directly after the form is shown, Repeat-Until loop starts, which keeps checking for the variable ModalResult to become a value greater than zero. When this occurs, the final code closes the form.

在TForm的ShowModal函数中,直接在显示表单之后,开始Repeat-Until循环,该循环不断检查变量ModalResult变为大于零的值。 发生这种情况时,最终代码将关闭表单。

You can set ModalResult at design-time, as described above, but you can also set the form's ModalResult property directly in code at run-time.

您可以如上所述在设计时设置ModalResult,但也可以在运行时直接在代码中设置表单的ModalResult属性。

翻译自: https://www.thoughtco.com/communicating-between-forms-4092543

表单之间值传递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值