当你需要访问其他窗体的状态时,你只须给访问的组件属性定义一个窗体属性。要实现这一点,可以通过读取组件状态的Get方法和设置组件状态的Set方法实现。假如你现在需

  • 主题发起人 主题发起人 dxydudugo
  • 开始时间 开始时间
D

dxydudugo

Unregistered / Unconfirmed
GUEST, unregistred user!
当你需要访问其他窗体的状态时,你只须给访问的组件属性定义一个窗体属性。要实现这一点,可以通过读取组件状态的Get方法和设置组件状态的Set方法实现。假如你现在需要改变用户界面,用另外一个组件替换现有的组件,那么你只需做的是修改与这个组件属性相关的Get方法和Set方法,我在哪申明 (20分)<br />private
function GetText:String;
procedure SetText(const Value:String);
public
property Text:String; read GetText write SetText;
function TformDialog.GetText:String;
begin
Result:=Edit1.Text;
end;
procedure TformDialog.SetText(const Value:String);
begin
Edit1.Text;=Value;
end;
 
你说的是不是 两个窗体中的对象互相访问 ,look

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(form2.Edit2.Text );
end;

end.
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Edit2: TEdit;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses unit1;

{$R *.dfm}

procedure TForm2.Button2Click(Sender: TObject);
begin
showmessage(form1.Edit1.Text );
end;

end.


 
对不对呀??
 
你说的好像是面向对象编程的一些基本原则, 对不对呀??

规则11:显示组件属性(Expose Components Properties) 当你需要访问其他窗体的状态时,你不应该直接访问它的组件。因为这样会将其他窗体或其它类的代码和用户界面结合在一起,而用户界面往往是一个应用程序中最容易发生改变的部分。最好的方法是,为你需要访问的组件属性定义一个窗体属性。要实现这一点,可以通过读取组件状态的Get方法和设置组件状态的Set方法实现。 假如你现在需要改变用户界面,用另外一个组件替换现有的组件,那么你只需做的是修改与这个组件属性相关的Get方法和Set方法,而不必查找,修改所有引用这个组件的窗体和类的源码。详细实现方法请参见下面的代码:
private
function GetText:String;
procedure SetText(const Value:String);
public
property Text:String;
read GetText write SetText;
function TformDialog.GetText:String;
begin
Result:=Edit1.Text; end;
procedure TformDialog.SetText(const Value:String); begin Edit1.Text;=Value;
end;
 
unit MainFrm;

interface

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

type
TMainForm = class(TForm)
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
// FText: String;
{ Private declarations }
// function GetText:String;
// procedure SetText(const Value:String);

public
{ Public declarations }
// property Text:String read FText write SetText;
function GetText:String;
procedure SetText(const Value:String);
end;

var
MainForm: TMainForm;

implementation

uses Unit1, Unit2;

{$R *.dfm}

procedure TMainForm.Button1Click(Sender: TObject);
begin
form1.show;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin

form2.show;
end;



function TMainForm.GetText: String;
begin
Result:=Edit2.Text;
end;

procedure TMainForm.SetText(const Value: String);
begin
Edit2.Text:=Value;
end;

end.

{
private
function GetText:String;
procedure SetText(const Value:String);
public
property Text:String;read GetText write SetText;


function TformDialog.GetText:String;
begin
Result:=Edit1.Text;
end;

procedure TformDialog.SetText(const Value:String);
begin
Edit1.Text;=Value;
end;


###################3
unit Unit2;

interface

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

type
TForm2 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses MainFrm;

{$R *.dfm}

procedure TForm2.FormDblClick(Sender: TObject);
begin
showmessage(MainForm.GetText );
end;

end.
¥#######################
unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

uses MainFrm;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin

showmessage(MainForm.GetText );
end;

end.
 
简单的说,主窗体的界面改变,只需修改主窗体,而不用修改所有的引用主窗体的窗体
 
后退
顶部