问题描述
我从一个表单创建并显示第二个表单。从第二种形式,我想更新第一种形式的控件。但是我遇到访问冲突。我可以使它与自动创建中的表单一起使用,但是当我使用create方法创建表单时却无法使用它,这会导致违规。
From a form, I create and show a second form. From the second form I want to update a control on the first form. But I get access violations. I can get it to work with the form in the autocreate, but not when I create the form with the create method I get violations.
下面是一个示例。如果我在自动创建中使用窗体11来运行它,那么它将起作用(我在第一个窗体中更新了按钮标题)。但是,如果在单元10中,如果我注释了form11.show ;,并且取消了对create和show的注释,然后将Form11从自动创建中删除,则会出现访问冲突。
Below is an example. If I run it as it is with form 11 in autocreate it works (I update a button caption in the first form). But, if in unit 10, if I comment out form11.show;, and I uncomment the create and the show and then take Form11 out of autocreate, I get an access violation.
问题-使用create方法创建表单时,如何从显示的表单中更新父表单。
Question - How can I update the parent form from the showed form when I create the form with a create method.
Unit10
unit Unit10;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm10 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form10: TForm10;
implementation
uses Unit11;
{$R *.dfm}
procedure TForm10.Button1Click(Sender: TObject);
var
fForm : TForm11;
Begin
// fForm := Form11.Create(Self); //This and next line give me access violation
// fForm.Show; // with form11 out of autocreate
form11.show; //This works with form11 in the autocreate.
end;
end.
Unit11
unit Unit11;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm11 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form11: TForm11;
implementation
uses unit10;
{$R *.dfm}
procedure TForm11.Button1Click(Sender: TObject);
begin
form10.button1.caption := 'Changed';
end;
end.
推荐答案
这是不正确的:
fForm := Form11.Create(Self)
应该是这样的:
fForm := TForm11.Create(Self)
即 TForm11
,而不是 Form11
。要创建对象,必须通过类调用构造函数。
That is, TForm11
, not Form11
. To create an object, you have to call the constructor via the class.
这篇关于Delphi从子窗体更新父窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!