Solved

event handling of dynamic controls

Posted on 2004-04-05
11
206 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 228
Using idhttp to login to instagram 2 64
Printing problem 2 72
Delphi inherited method 6 37
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

14 Experts available now in Live!

Get 1:1 Help Now