同时更改所有对象中的信息

时间:2014-01-29 20:30:51

标签: class delphi oop object delphi-xe

我对物体有点新意,我一直坚持这个,我希望你们能帮助我。

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

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

表格中有5个按钮:

  1. 左侧G1 - 创建一个按钮,将表格左侧的一个分组
  2. 右G2 - 创建一个按钮,将表格右侧的两个分组
  3. 添加标题 - 为独立于该组创建的所有按钮添加标题(与所有标题相同)
  4. 启用G1 - 启用属于第一组的所有按钮
  5. 禁用G1 - 禁用属于第一组的所有按钮
  6. 我想要做的是能够为每个不同的组创建我想要的多个按钮,然后一次更改所有标题并一次启用和禁用单独的组,这是来自更大的应用程序的示例项目如果创建了大量的对象,那么表单中的所有对象都会非常消耗,如果对象不是通过主窗体进行更改,那将会很好。

    请大家好,我不希望任何人为我做这项工作,我希望有人指出我正确的方向,我是否应该使用一个类,即使知道我不能改变对象的个别属性是有办法吗?我可以以某种方式在对象上实现它,还是我需要在调用这些对象的单元上实现它。任何指针?

    非常感谢提前。

    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.
    

1 个答案:

答案 0 :(得分:0)

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

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

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

谢谢,我希望将来可以帮助其他人。

   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.