Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

event handling of dynamic controls

Posted on 2004-04-05
11
Medium Priority
?
216 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 196 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 192 total points
ID: 10758120
Errr.. What'yer doin? Just use

StringGrid1.OnClick := Grid1_Click;

That's all..!
0
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!

 
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 192 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

Technology Partners: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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