Solved

event handling of dynamic controls

Posted on 2004-04-05
11
210 Views
Last Modified: 2010-04-05
i have created 12 stringgrids now i want to trap there events such as click and double click how should i do ? please help ?
0
Comment
Question by:chotrani_mukesh
  • 5
  • 2
11 Comments
 
LVL 12

Accepted Solution

by:
esoftbg earned 49 total points
ID: 10756654
unit Unit_Q_20943709;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    private { Private declarations }
    public  { Public declarations }
    published
      procedure Grid1_Click(Sender: TObject);
      function  Get_Method_By_Name(Method_Name: string): TNotifyEvent;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.Get_Method_By_Name(Method_Name: string): TNotifyEvent;
begin
  @Result := MethodAddress(Method_Name);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FOnClick1:     TNotifyEvent;
begin
  FOnClick1 := nil;
  try
  FOnClick1 := Get_Method_By_Name('Grid1_Click');
  finally
    StringGrid1.OnClick := FOnClick1;
  end;
end;

procedure TForm1.Grid1_Click(Sender: TObject);
begin
  ShowMessage('StringGrid1 is just Clicked');
end;

end.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10756707
it is a better example (with runtime created StringGrid1) :

unit Unit_Q_20943709;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    private { Private declarations }
    public  { Public declarations }
    published
      procedure Grid1_Click(Sender: TObject);
      function  Get_Method_By_Name(Method_Name: string): TNotifyEvent;
  end;

var
  Form1: TForm1;
  StringGrid1: TStringGrid;

implementation

{$R *.dfm}

function TForm1.Get_Method_By_Name(Method_Name: string): TNotifyEvent;
begin
  @Result := MethodAddress(Method_Name);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FOnClick1:     TNotifyEvent;
begin
  StringGrid1 := TStringGrid.Create(Self);
  StringGrid1.Parent := Self;
  StringGrid1.Left := 16;
  StringGrid1.Top := 16;
  FOnClick1 := nil;
  try
  FOnClick1 := Get_Method_By_Name('Grid1_Click');
  finally
    StringGrid1.OnClick := FOnClick1;
  end;
end;

procedure TForm1.Grid1_Click(Sender: TObject);
begin
  ShowMessage('StringGrid1 is just Clicked');
end;

end.
0
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 48 total points
ID: 10758120
Errr.. What'yer doin? Just use

StringGrid1.OnClick := Grid1_Click;

That's all..!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:esoftbg
ID: 10758686
unit Unit_Q_20943709;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    private { Private declarations }
    public  { Public declarations }
    published
      procedure Grid1_Click(Sender: TObject);
      procedure Grid2_Click(Sender: TObject);
  end;

var
  Form1: TForm1;
  StringGrid1: TStringGrid;
  StringGrid2: TStringGrid;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1 := TStringGrid.Create(Self);
  StringGrid1.Parent := Self;
  StringGrid1.Height := 144;
  StringGrid1.Width := 352;
  StringGrid1.Left := 16;
  StringGrid1.Top := 16;
  @StringGrid1.OnClick := MethodAddress('Grid1_Click');

  StringGrid2 := TStringGrid.Create(Self);
  StringGrid2.Parent := Self;
  StringGrid2.Height := 144;
  StringGrid2.Width := 352;
  StringGrid2.Left := 16;
  StringGrid2.Top := 16;
  @StringGrid2.OnClick := MethodAddress('Grid2_Click');
end;

procedure TForm1.Grid1_Click(Sender: TObject);
begin
  ShowMessage('StringGrid1 is just Clicked');
end;

procedure TForm1.Grid2_Click(Sender: TObject);
begin
  ShowMessage('StringGrid2 was just Clicked');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  StringGrid1.Destroy;
  StringGrid2.Destroy;
end;

end.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10758701
 StringGrid2.Top := 160;
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 10758782
esoftbg,

Your StringGrid1 and 2 variable should really be part of the TForm1 class.

And I still don't understand why you're doing this MethodAddress() business... is there some reason?
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 48 total points
ID: 10758898
just listening . . .

for assigning events:

with TSomeClass.Create(AOwner) do
begin
   SomeProperty := somevalue;
   SomeEvent := PredefinedEventMethod;
   Name := SomeIdentifier;
end;

procedure PredefinedEventMethod(sender : TObject, ...maybeMorePrams...) ;
begin
  if (Sender is TSomeClass) then
    if TSomeClass(Sender).Name = SomeIdentifier then
      DoThisForThisInstance;
   .... to be continued ...
end;

just as abstract sample

meikl ;-)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10758920
Hi andrewjb, I hope this time all is o.k.

unit Unit_Q_20943709;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    private { Private declarations }
    public  { Public declarations }
      StringGrid1: TStringGrid;
      StringGrid2: TStringGrid;
    published
      procedure Grid1_Click(Sender: TObject);
      procedure Grid2_Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1 := TStringGrid.Create(Self);
  StringGrid1.Parent := Self;
  StringGrid1.Height := 144;
  StringGrid1.Width := 352;
  StringGrid1.Left := 16;
  StringGrid1.Top := 16;
  StringGrid1.OnClick := Grid1_Click;

  StringGrid2 := TStringGrid.Create(Self);
  StringGrid2.Parent := Self;
  StringGrid2.Height := 144;
  StringGrid2.Width := 352;
  StringGrid2.Left := 16;
  StringGrid2.Top := 160;
  StringGrid2.OnClick := Grid2_Click;
end;

procedure TForm1.Grid1_Click(Sender: TObject);
begin
  ShowMessage('StringGrid1 is just Clicked');
end;

procedure TForm1.Grid2_Click(Sender: TObject);
begin
  ShowMessage('StringGrid2 was just Clicked');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  StringGrid1.Destroy;
  StringGrid2.Destroy;
end;

end.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question