Solved

event handling of dynamic controls

Posted on 2004-04-05
11
207 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now