Solved

How do I setup a "onClick" event without using the object inspector?

Posted on 2002-06-28
2
286 Views
Last Modified: 2011-09-20
I have a form that has 56 charts on it.  I defined the charts as an array and set the properties in the code snippet below:

var
  TestProgressForm: TTestProgressForm;
  Charts : array[1..MAX_WFR_COL,1..MAX_WFR_ROW] of Tchart

Procedure TTestProgressForm.AssignChartProperties;
var col,row : integer;
    ChartSeries : TChartSeries;
begin
  for col := 1 to MAX_WFR_COL do begin
    for row := 1 to MAX_WFR_ROW do begin
      Charts[col,row] := Tchart.Create(TestProgressForm);
      Charts[col,row].Parent := TestProgressForm;
      BarSeries[col,row] := TBarSeries.Create(self);
      Charts[col,row].AddSeries(BarSeries[col,row]);
      charts[col,row].Enabled := TRUE;
      charts[col,row].Width := 80;
      charts[col,row].Height := 80;
      {set up properties here}
      charts[col,row].Top := 15 + (row-1)*(charts[col,row].Height + 5);
      charts[col,row].Left := 55 + (col-1)*(charts[col,row].Width + 5);
      with charts[col,row].Series[0] as TBarSeries do begin
        Clear;
        CustomBarWidth := 10;
        title := '';
        Marks.Visible := FALSE;
        ColorEachPoint := True;
        Marks.Style := smsPercent;
        Visible := False ;
        ShowInLegend := False;
        AutoMarkPosition := False;
        HorizAxis := aBottomAxis;
        VertAxis := aLeftAxis;
       end;
    end;
  end;
end;

What I would like to do is set up the code so that when I click or double click on the chart a new form is displayed showing the details.  The new form would be called TestDetails and would need to know which chart was double clicked on.   I do not how to go about defining this.

Thank you,
p
0
Comment
Question by:PattiGaultney
2 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 7117693
Hi,

1. In your form declaration add a new procedure declaration:

type
TTestProgressForm =
 ...
 public
  ...
  procedure ChartsClick(Sender: TObject); // common to all charts clicks
 ...
end;

2. Write that procedure in the 'implementation' section

procedure TTestProgressForm.ChartsClick(Sender: TObject);
begin
  if (Sender is TChart) then begin
    // Assign the chart to a property of TestDetails form
    TestDetails.TheChart := (Sender as TChart);
    // Show the form
    TestDetails.Show;
  end;
end;

3. Add next line somewhere in your loop above (when creating the charts:
  // This line assigns ChartsClick procedure as OnClick event handler (their declarations must be the same)
  charts[col,row].OnClick := ChartsClick;

4. In TestDetails form:

TestDetails =
private
  FChart: TChart; // new private field
....
public
  property TheChart: TChart read FChart write FChart; // new public property to hold a reference to a chart
...
end;

Now, in the events of TestDetails form you can use 'TheChart' to obtain a reference to the chart that has been clicked last. For example:

with TheChart.Series[0] as TBarSeries do begin
  ...
end;

Perhaps there is an easier way but that's coming up in my mind now and it's almost 5 AM here. If you need more info, please ask.

Regards, Geo
0
 
LVL 3

Expert Comment

by:sburck
ID: 7118983
I think it would be easier just to use some determining code in the OnClick handler... like this:

if (Sender as TChart).Name = "Chart1" then...

If there is position information, where you can determine
which chart is being accessed by it's X and Y coordinates, you can do that too, like

ChartID : integer;

ChartID = Idfunc((Sender as TChart).Left,(Sender as TChart).Bottom);

Where IDfunc returns the index as a function of left and right.

Finally, if you use the tag field when you create, putting in the number of the chart, then you can just do this:

...during creation of the charts
Charts[col,row].tag = col*100+row;

...during the OnClick
switch(Sender as TChart).tag)
...



0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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