Question

I'm a bit new with objects and I have really have been stuck on this, I hope you guys can help me.

I create a simple application to exemplify the problem I am having with Delphi.

I have an object that inherited from TButton, but in creation time I can assign a "Group" to this button, and after I want to be able to change the caption of all button created and to enable and disable it depending on the group that it belongs.

There are 5 button in the form:

  1. Left G1 - Create a button to group one in the left side the form
  2. Right G2 - Create a button to group two in the right side the form
  3. Add caption - Add caption to all button created independent of the group (same caption to all)
  4. Enable G1 - Enable all button that belongs to group one
  5. Disable G1 - Disable all button that belongs to group one

What I want to do is to be able to create as many buttons I want for each different group and then change all the captions at once and enable and disable separate groups at once, this is a sample project from a much bigger application that have lots of objects created so go trough all the objects in the form would be very consuming it would be nice if the change could be made by the object not by the main form.

Please guys I don't want to anyone to make the work for me, I want someone to point me in the right direction, should I use a class even knowing that I can't change the individual properties of the objects is there a way around? Can I somehow implement it on the object or do I need to implement it on the unit calling those objects. Any pointer?

Many thanks in advance.

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.
Was it helpful?

Solution

Ok, after the hint from David I find the solution:

First I create an array the same kind of the object Then I create a class function that create this object and save into this array Now every time I need to access any of those objects I can just browse this list and change what need to be change.

Why I did this way? Before I was browsing all objects in my main form what was taking a lot of processing, doing this way I just have to browse objects of the kind I want to change.

Thanks I hope it can help others in the future.

   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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top