【问题标题】:Changing information in all Objects the same time同时更改所有对象中的信息
【发布时间】:2014-01-29 20:30:50
【问题描述】:

我对对象有点陌生,我真的一直坚持这一点,希望你们能帮助我。

我创建了一个简单的应用程序来举例说明我在使用 Delphi 时遇到的问题。

我有一个从 TButton 继承的对象,但是在创建时我可以为这个按钮分配一个“组”,之后我希望能够更改所有创建的按钮的标题并启用和禁用它,具体取决于它所属的组。

表单中有5个按钮:

  1. 左 G1 - 创建一个按钮以在表单左侧进行分组
  2. 右 G2 - 在表单右侧创建一个按钮以将两个分组
  3. 添加标题 - 为独立于组创建的所有按钮添加标题(对所有人都使用相同的标题)
  4. 启用 G1 - 启用属于第一组的所有按钮
  5. 禁用 G1 - 禁用属于第一组的所有按钮

我想要做的是能够为每个不同的组创建尽可能多的按钮,然后一次更改所有标题并同时启用和禁用单独的组,这是来自更大应用程序的示例项目创建了很多对象,因此遍历表单中的所有对象将非常消耗,如果可以由对象而不是主表单进行更改,那就太好了。

请伙计们,我不想让任何人为我工作,我希望有人指出我正确的方向,我是否应该使用一个类,即使我知道我无法更改对象的各个属性是有办法吗?我可以以某种方式在对象上实现它,还是需要在调用这些对象的单元上实现它。任何指针?

非常感谢。

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TSample = class(TButton)
  private
    class var count: integer;
  protected
  public
    procedure increseCount;
    constructor Create(AOwner: TComponent; Ypos, Group:Integer); overload;
    class procedure rename(name: string);
    class procedure enableGroup(Group: Integer; value: Boolean);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

constructor TSample.Create(AOwner: TComponent; Ypos, Group:Integer);
begin
   self.Create(AOwner);
   self.Top := count *50;
   self.Left := Ypos;
   self.Tag := Group;
   increseCount;
   self.Parent := AOwner as TWinControl;
end;

procedure TSample.increseCount;
begin
  count := count + 1;
end;

class procedure TSample.enableGroup(Group: Integer; value: Boolean);
begin
  //???
end;
class procedure TSample.rename(name: string);
begin
  //self.Caption := name; ???
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  left: TSample;
begin
  left := TSample.Create(self, 24, 1);
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  right: TSample;
begin
  right := TSample.Create(self, 200, 2);
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
  TSample.rename('Oi!');
end;

procedure TForm2.Button4Click(Sender: TObject);
begin
  TSample.enableGroup(1, True);
end;

procedure TForm2.Button5Click(Sender: TObject);
begin
  TSample.enableGroup(1, False);
end;

end.

【问题讨论】:

  • 你有什么问题?
  • 我发布的示例中如何更改标题以及如何启用和禁用属于某个组的按钮。
  • 您希望我们为您编写代码吗?你还没有做出任何真正的努力。代码如何找到同一组中的所有控件?
  • 不,我不想要代码,我发布了一个代码,我想知道我应该使用什么以及如何使用,因为类方法不是可行的方法,因为它无法更改此属性,如果有人给我一个提示,我可以添加到我已经做过的示例中,然后在此处重新发布结果,为其他人添加新的参考。
  • 您想如何找到其他控件?为什么要使用类方法?

标签: class delphi oop object delphi-xe


【解决方案1】:

好的,在大卫的提示下,我找到了解决方案:

首先我创建一个与对象相同类型的数组 然后我创建一个类函数来创建这个对象并保存到这个数组中 现在,每次我需要访问这些对象中的任何一个时,我都可以浏览此列表并更改需要更改的内容。

我为什么要这样做?在我浏览主窗体中的所有对象之前,需要进行大量处理,这样做我只需要浏览我想要更改的类型的对象。

谢谢,希望以后能帮到别人。

   unit Unit2;

    interface

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

    type
      TForm2 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    type
      TSample = class(TButton)
      private
        class var count: integer;
        class var List: Array of TSample;
      protected
      public
        procedure increseCount;
        constructor Create(AOwner: TComponent; Ypos, Group:Integer); overload;
        class function CreateInstance(AOwner: TComponent; Ypos, Group:Integer): TSample; overload;
        class procedure rename(name: string);
        class procedure enableGroup(Group: Integer; value: Boolean);
      end;

    var
      Form2: TForm2;

    implementation

    {$R *.dfm}

    class function TSample.CreateInstance(AOwner: TComponent; Ypos, Group:Integer): TSample;
    var
     i: Integer;
    begin
      i := Length(List)+1;
      SetLength(List, i);
      List[i-1] := self.Create(AOwner, Ypos, Group);
    end;


    constructor TSample.Create(AOwner: TComponent; Ypos, Group:Integer);
    begin
       self.Create(AOwner);
       self.Top := count *50;
       self.Left := Ypos;
       self.Tag := Group;
       increseCount;
       self.Parent := AOwner as TWinControl;
    end;

    procedure TSample.increseCount;
    begin
      count := count + 1;
    end;

    class procedure TSample.enableGroup(Group: Integer; value: Boolean);
    var
      i: Integer;
    begin
      for i := 0 to Length(List)-1 do
        if List[i].Tag = Group then
          List[i].Enabled := value;
    end;

    class procedure TSample.rename(name: string);
    var
      i: Integer;
    begin
      for i := 0 to Length(List)-1 do
        List[i].Caption := name;
    end;

    procedure TForm2.Button1Click(Sender: TObject);
    var
      left: TSample;
    begin
      left := TSample.CreateInstance(self, 24, 1);
    end;

    procedure TForm2.Button2Click(Sender: TObject);
    var
      right: TSample;
    begin
      right := TSample.CreateInstance(self, 200, 2);
    end;

    procedure TForm2.Button3Click(Sender: TObject);
    begin
      TSample.rename('Oi!');
    end;

    procedure TForm2.Button4Click(Sender: TObject);
    begin
      TSample.enableGroup(1, True);
    end;

    procedure TForm2.Button5Click(Sender: TObject);
    begin
      TSample.enableGroup(1, False);
    end;

    end.

【讨论】:

  • 不想告诉你,但这不是一个很好的解决方案——它有点程序/OO 混乱。类过程/函数/变量在某些情况下很有用,但您没有使用它们来尝试使其表现得像过程代码。假设您只影响此表单上的按钮,您最好在表单上有一个 TList (甚至是一个 TList 数组,在这些列表中添加/删除按钮并拥有一个采用 List 的过程 并将操作应用于整个列表。作为首选项,我还避免给数组一个 var name 'List' - 这很混乱。
  • 嗨,马特,谢谢你的支持,我在 OO 方面没有太多经验,所以这是造成混乱的主要原因。你说的我只是有一个问题,这是一个简化的例子,在我的实际程序中,我有相同的对象以不同的形式和单位使用,但对象是相同的(为了简化,假设它是一个主窗体上的按钮,但里面的所有子窗体都可以根据发生的情况启用或禁用)这就是为什么我决定将代码放在对象中的原因。在这种情况下,您有什么建议?我非常愿意将我的代码做得更好。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多