2014-01-24 62 views
-5

表格10创建并显示form11和form11创建并显示form12。然后,form12尝试更新form10(成功)上的控件,form11(访问冲突)上的控件和form12(成功)上的控件。为什么我会更新中间格式form11的访问冲突。评论陈述30和31不起作用,我想知道为什么请。 30正在更新中间格式。 31是旁边的,没有关系,但它不起作用,我不知道为什么炸弹。更新从不工作的孩子的孩子

1: unit Unit10; 
2: 
3: interface 
4: 
5: uses 
6: Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
7: Dialogs, StdCtrls; 
8: 
9: type 
10: TForm10 = class(TForm) 
11: Button1: TButton; 
12: procedure Button1Click(Sender: TObject); 
13: private 
14: { Private declarations } 
15: public 
16: { Public declarations } 
17: end; 
18: 
19: var 
20: Form10: TForm10; 
21: 
22: implementation 
23: uses Unit11; 
24: 
25: {$R *.dfm} 
26: 
27: procedure TForm10.Button1Click(Sender: TObject); 
28: var 
29: fForm11 : TForm11; 
30: begin 
31: fForm11 := TForm11.Create(Application); 
32: fForm11.show; 
33: end; 
34: 
35: end. 

1: unit Unit11; 
2: 
3: interface 
4: 
5: uses 
6: Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
7: Dialogs, StdCtrls; 
8: 
9: type 
10: TForm11 = class(TForm) 
11: Button1: TButton; 
12: procedure Button1Click(Sender: TObject); 
13: private 
14: { Private declarations } 
15: public 
16: { Public declarations } 
17: end; 
18: 
19: var 
20: Form11: TForm11; 
21: 
22: implementation 
23: uses Unit12, Unit10; 
24: 
25: {$R *.dfm} 
26: 
27: procedure TForm11.Button1Click(Sender: TObject); 
28: var 
29: fForm12 : TForm12; 
30: begin 
31: form10.Button1.Caption := 'done'; 
32: fForm12 := TForm12.Create(Self); 
33: fForm12.show; 
34: end; 
35: 
36: end. 

1: unit Unit12; 
2: 
3: interface 
4: 
5: uses 
6: Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
7: Dialogs, StdCtrls; 
8: 
9: type 
10: TForm12 = class(TForm) 
11: Button1: TButton; 
12: procedure Button1Click(Sender: TObject); 
13: private 
14: { Private declarations } 
15: public 
16: { Public declarations } 
17: end; 
18: 
19: var 
20: Form12: TForm12; 
21: 
22: implementation 
23: uses Unit11, Unit10; 
24: 
25: {$R *.dfm} 
26: 
27: procedure TForm12.Button1Click(Sender: TObject); 
28: begin 
29: Form10.Button1.Caption := 'Unit10'; 
30: //Form11.Button1.Caption := 'Unit11'; //get an access violation 
31: //Form12.Button1.Caption := 'Unit12'; //get an access violation   
32: Button1.Caption := 'Unit12';   //this same as 31 without Form12 prefix 
33: end; 
34: 
35: end. 
+2

我们很乐意为您提出新手问题,但我们希望您阅读答案并留意。 –

回答

4

您需要更多关注您的变量名称,声明和使用。

您在发布的代码中有两个与Form11相关的完全独立的变量。

首先是全球Form11由IDE自动添加(应该将其删除,如果你不是自动创建的形式,因为这是其在第一时间存在的唯一理由),在宣布的Unit11.pasinterface部分:

type 
TForm11 = class(TForm) 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
private 
{ Private declarations } 
public 
{ Public declarations } 
end; 

var 
    Form11: TForm11 

二是本地变量命名fForm11您在Unit10.pas宣布,在您按一下按钮处理程序:

procedure TForm10.Button1Click(Sender: TObject); 
var 
    fForm11 : TForm11; 
begin 

第二个是你分配形式的实际情况下,你创建一个:

procedure TForm10.Button1Click(Sender: TObject); 
var 
    fForm11 : TForm11; 
begin 
    fForm11 := TForm11.Create(Application); // Assigns to the *local* fForm11 
    fForm11.show;        // Shows this copy of the form 
end; 

一个你在Unit12使用,你到哪儿去评论它,并提到,这导致访问冲突是全球Form11,这是唯一一个在这个代码是可见:

procedure TForm12.Button1Click(Sender: TObject); 
begin 
    Form10.Button1.Caption := 'Unit10'; 
    //Form11.Button1.Caption := 'Unit11'; //get an access violation 
    //Form12.Button1.Caption := 'Unit12'; //get an access violation   
    Button1.Caption := 'Unit12';   //this same as 31 without Form12 prefix 
end; 

的问题是,你从来没有创建0个实例并将其分配给Form11;您创建并将其分配给本地fForm11而不是根本不在范围内。

修复应该清楚:删除局部变量,并将您创建的窗体分配给适当的变量。

但是,适当的修复方法是自动创建表单,因为您非常肆意地在各处使用它们。由于模块间依赖关系的这种不良使用,您最终会一次又一次地遇到相同类型的问题,而当您试图访问它们时,事情不会被创建。

+0

非常感谢您的帮助,您的指导帮助我解决了访问违规问题。 – Owen

+2

@OwenH你可以通过接受Ken的答案来显示你的感谢,点击它左边的复选标记。 –