我经常需要在Delphi / C ++ Builder中设计一个对话框,允许修改对象的各种属性,使用它的代码通常如下所示。

Dialog.Edit1.Text := MyObject.Username;
Dialog.Edit2.Text := MyObject.Password;
// ... many more of the same

if (Dialog.ShowModal = mrOk) 
begin
  MyObject.Username := Dialog.Edit1.Text;
  MyObject.Password := Dialog.Edit2.Text;
  // ... again, many more of the same
end;

我还经常需要类似的代码来将对象编组到/从xml / ini-files /中。

是否有任何常用的习惯用法或技巧可以避免这种简单但重复的代码?

有帮助吗?

解决方案

好吧,我认为非常宝贵的是 GExperts 插件向导“反向声明”。通过按Shift + ALT + R

安装GExperts后调用

它的作用是自动切换突出显示的块的分配。例如:

edit1.text := dbfield.asString;

变为

dbField.asString := edit1.text;

不完全符合您的要求,但是当您进行大量任务时可节省大量时间。

其他提示

这是我的变化。我做了什么,厌倦了相同的重复代码,是根据我想要的XML节点名称命名所有编辑框,然后迭代组件并输出它们的值。 XML代码应该是显而易见的,我只有一个编辑和复选框,但你应该能够看到这个想法。

procedure TfrmFTPSetup.LoadFromXML(szFileName : string);
var
xComponent : TComponent;
nLoop : Integer;
xMainNode : TXmlNode;
xDocument : TNativeXml;
begin
inherited;

xDocument := TNativeXml.Create;
try
    xDocument.LoadFromFile(szFileName);
    xMainNode := xml_ChildNodeByName(xDocument.Root, 'options');
    for nLoop := 0 to ComponentCount - 1 do
    begin
        xComponent := Components[nLoop];
        if xComponent is TRzCustomEdit then
        begin
            (xComponent as TRzCustomEdit).Text := xMainNode.AttributeByName[xComponent.Name];
        end;
        if xComponent is TRzCheckBox then
        begin
            (xComponent as TRzCheckBox).Checked := xml_X2Boolean(xMainNode.AttributeByName[xComponent.Name], false);
        end;
    end;
finally
    FreeAndNil(xDocument);
end;
 end;

   procedure TfrmFTPSetup.SaveToXML(szFileName : string);
var
xComponent : TComponent;
nLoop : Integer;
xMainNode : TXmlNode;
xDocument : TNativeXml;
begin
inherited;

xDocument := TNativeXml.CreateName('ftpcontrol');
try
    xMainNode := xml_ChildNodeByNameCreate(xDocument.Root, 'options');
    for nLoop := 0 to ComponentCount - 1 do
    begin
        xComponent := Components[nLoop];
        if xComponent is TRzCustomEdit then
        begin
            xMainNode.AttributeByName[xComponent.Name] := (xComponent as TRzCustomEdit).Text;
        end;
        if xComponent is TRzCheckBox then
        begin
            xMainNode.AttributeByName[xComponent.Name] := xml_Boolean2X((xComponent as TRzCheckBox).Checked);
        end;
    end;

    xDocument.XmlFormat := xfReadable;
    xDocument.SaveToFile(szFileName);
finally
    FreeAndNil(xDocument);
end;
 end;

在表单上访问可视组件的属性并不是一种好习惯。具有单独的属性被认为是更好的。在上面的示例中,您将拥有带有get和set方法的用户名和密码属性。

例如:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
  private
    function GetPassword: string;
    function GetUsername: string;
    procedure SetPassword(const Value: string);
    procedure SetUsername(const Value: string);
  public
    property Password: string read GetPassword write SetPassword;
    property Username: string read GetUsername write SetUsername;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetPassword: string;
begin
 Result := Edit2.Text;
end;

function TForm1.GetUsername: string;
begin
 Result := Edit1.Text;
end;

procedure TForm1.SetPassword(const Value: string);
begin
  Edit2.Text := Value;
end;

procedure TForm1.SetUsername(const Value: string);
begin
  Edit1.Text := Value;
end;

end.

这意味着您可以更改表单上的可视组件,而不会影响调用代码。

另一种选择是将对象作为属性传递给对话框;

unit Unit1;

interface

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

type
  TUserObject = class(TObject)
  private
   FPassword: string;
   FUsername: string;
  public
   property Password: string read FPassword write FPassword;
   property Username: string read FUsername write FUsername;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    btnOK: TButton;
    procedure btnOKClick(Sender: TObject);
  private
    FUserObject: TUserObject;
    procedure SetUserObject(const Value: Integer);
  public
    property UserObject: Integer read FUserObject write SetUserObject;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnOKClick(Sender: TObject);
begin
 FUserObject.Username := Edit1.Text;
 FUserObject.Password := Edit2.Text;
 ModalResult := mrOK;
end;

procedure TForm1.SetUserObject(const Value: Integer);
begin
 FUserObject := Value;
 Edit1.Text := FUserObject.Username;
 Edit2.Text := FUserObject.Password;
end;

end.

希望有所帮助。

Delphi至少有'With',虽然它没有完全解决问题。

if (Dialog.ShowModal = mrOk) 
begin
  with MyObject do
  begin
    Username := Dialog.Edit1.Text;
    Password := Dialog.Edit2.Text;
    // ... again, many more of the same
  end;
end;

建筑师AFAIK没有任何相似之处。

将控件绑定到数据在Delphi中运行良好,但不幸的是,只有当数据驻留在TDataSet后代时。您可以编写一个使用对象进行数据存储的TDataSet后代,事实证明已经存在这样的事情。请参阅下面的链接...此实现似乎仅适用于对象集合(TCollection或TObjectList),而不是单个对象。

http://www.torry.net/pages.php?id=563 - 在页面中搜索“Snap Object DataSet”

我对此没有任何个人经验,但如果它有用,它将非常有用,特别是如果它也适用于单个对象实例,例如数据模块上的属性......

查找“中介模式”。这是一个GoF设计模式,在他们的书中,GoF实际上激发了这种设计模式,其形式与你在这里描述的情况有些相似。它旨在解决一个不同的问题 - 耦合 - 但我认为你无论如何都有这个问题。

简而言之,我们的想法是创建一个对话框中介,一个位于所有对话框小部件之间的额外对象。没有小部件知道任何其他小部件,但每个小部件都知道中介。调解员知道所有小部件。当一个小部件发生更改时,它会通知中介者;然后,调解员会通知相关小部件。例如,当您单击“确定”时,调解器可以通知其他小部件有关此事件的信息。

这样,每个小部件都只处理与自身相关的事件和动作。调解器负责所有小部件之间的交互,因此所有这些“样板”都是代码被分割在所有小部件上,并且“残留”部分被分配。对所有小部件来说都是全局的是交互,它是调解者的责任。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top