Solved

event handling of dynamic controls

Posted on 2004-04-05
11
209 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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