先从主Form上弹出一个从Form来输入实数,用来输入的从Form上的Edit控件是动态生成的(代码如下),接着把Edit上的输入的实数传回(50分)

  • 主题发起人 主题发起人 YL_YAN
  • 开始时间 开始时间
Y

YL_YAN

Unregistered / Unconfirmed
GUEST, unregistred user!
先从主Form上弹出一个从Form来输入实数,用来输入的从Form上的Edit控件是动态生成的(代码如下),接着把Edit上的输入的实数传回主Form(赋值给Label) ,运行时出错“Access violation at address 0000000E,read of address 0000000E', 这是为什么? 谢谢!


主Form的Pas文件如下:
unit Unit1;

interface

uses Math,Windows,SysUtils,CommDlg,CommCtrl,Penwin, WinTypes, WinProcs,
Messages, Classes, Graphics, Controls, Forms, Dialogs, Chart, Series,
ExtCtrls, Teengine, StdCtrls, Buttons,unit2;

type
TForm1 = class(TForm)
Image1: TImage;
Timer1: TTimer;
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
showmodal;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
RForm :TForm2;
Edit1 :TEdit;
begin
RForm:=TForm2.Create(Application);
Try
with RForm.Image1,Canvas do
begin
Textout(10,10,'Please input a real number:');
edit1:=tedit.Create(application);
edit1.Parent:=RForm;
edit1.Top:=10;
edit1.Left:=200;
s:=edit1.Text;
end;
with RForm do ShowModal;
finally RForm.Free;
end;
end;

end.




从Form的Pas文件如下:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;

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

var
Form2: TForm2;

implementation
uses Unit1;
{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
var
Edit1 :TEdit;
begin
Form1.Label1.Caption:=Edit1.Text;
close;
end;

end.
 
你把两个变量都命名为EDIT1,但他们还是两个变量,

procedure TForm2.Button1Click(Sender: TObject);
var
Edit1 :TEdit;
begin
Form1.Label1.Caption:=Edit1.Text;
close;
end;
这个EDIT1并不是你输入值的那个EDIT1
 
第1:你的代码很有问题,Unit1和Unit2存在交叉引用
第2:
procedure TForm2.Button1Click(Sender: TObject);
var
Edit1 :TEdit;
begin
Form1.Label1.Caption:=Edit1.Text;
close;
end;
Edit1只是你申明的一个对象,并没有创建,也没有引用任何一个对象的实例
当然会读地址错误了
 
var
Edit1 :TEdit;
begin
Form1.Label1.Caption:=Edit1.Text;
close;
end;
----------------------------------------------------------------
声明了TEdit类变量,却没创建对象,而这里的Edit1并非指向了form1里的对象。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

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

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
showmodal;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
RForm: TForm2;
Edit1: TEdit;
begin
RForm := TForm2.Create(Application);
try
with RForm.Image1,Canvas do
begin
Textout(10, 10, 'Please input a real number:');
edit1 := tedit.Create(RForm);
edit1.Parent := RForm;
edit1.Top := 10;
edit1.Left := 200;
end;
RForm.ShowModal;
label1.caption := RForm.InputValue;
finally
RForm.Free;
end;
end;

end.

///-----------------传说中的分隔符----------------------------

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm2 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
m_InputValue: string;
{ Private declarations }
public
{ Public declarations }
property InputValue:string read m_InputValue;
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
i:Integer;
begin
for i:=0 to self.ComponentCount -1 do
begin
if (Self.Components is TEdit) then
m_InputValue:=(Self.Components as TEdit).Text;
end;
Close;
end;


end.
 
这样写就可以了[:D]
发分发分
 
unit Unit1;

interface

uses Math,Windows,SysUtils,CommDlg,CommCtrl,Penwin, WinTypes, WinProcs,
Messages, Classes, Graphics, Controls, Forms, Dialogs, Chart, Series,
ExtCtrls, Teengine, StdCtrls, Buttons,unit2;

type
TForm1 = class(TForm)
Image1: TImage;
Timer1: TTimer;
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
showmodal;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
RForm :TForm2;
//------------
//在FORM2中声名
//Edit1 :TEdit;
//------------
begin
RForm:=TForm2.Create(Application);
Try
with RForm.Image1,Canvas do
begin
Textout(10,10,'Please input a real number:');
edit1:=tedit.Create(application);
edit1.Parent:=RForm;
edit1.Top:=10;
edit1.Left:=200;
s:=edit1.Text;
end;
with RForm do ShowModal;
finally RForm.Free;
end;
end;

end.




从Form的Pas文件如下:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;

type
TForm2 = class(TForm)
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public

{ Public declarations }
end;

var
Form2: TForm2;
//-------------
Edit1 :TEdit;
//---------------
implementation

uses Unit1;
{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.Label1.Caption:=Edit1.Text;
close;
end;

end.
 
to:大猪猪
这种方法没有解决交叉引用的问题,而多暴露出一个全局对象
建议不要这样写[:)]
 
DELPHI的FROM本来就自动支持交叉引用的问题
不本专门去结决
 
这个EDIT放在FROM2中
如果不用全局变量
那怎么能做到两个FROM都可访问到它?
 
if (Self.Components is TEdit) then
m_InputValue:=(Self.Components as TEdit).Text;
如果FROM2只有一个动态生成的TEDIT
你这个方法没问题
但是只要有两个以上的TEDIT
那你的方法所取的值并不一定是真正所需要的值
 
而且我已经试过,
动态生成的控件
Self.Components 是找不到的
不然就可以直接用Self.FindComponent(控件名)
更方便
 
支持交叉引用我们就应该去用吗?这是一种不好的代码习惯!
谁说动态生成的控件用Self.Components是找不到的,那看你怎么创建了
edit1 := tedit.Create(RForm);
这样就没问题
如果有多少TEdit,那么可以在别的地方想办法啊,比如Tag....
 
那就可以这样写了
procedure TForm2.Button1Click(Sender: TObject);
var
i:Integer;
begin
m_InputValue:=(Self.FindComponent('edit1')as TEdit).Text;
end;
Close;
end;
 
to 大猪猪
Edit1是对象名,而不是一个Name属性
 
赞成少用交叉引用
 
对象名默认是NAME
已经测验过
 
不管怎么样
既然是动态生成的控件
也不在乎设置一下NAME这个属性啊
 
我比较爱用Tag来做,呵呵,name不保险,比如之前已经有了一个Edit,而Name刚好为Edit1[:D]

PS:楼主不见了,变成我们俩讨论了
 
后退
顶部