Move Cursor Tool with arrow keys

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Move Cursor Tool with arrow keys

Post by tbonejo » Wed Jan 31, 2007 1:19 am

I have 2 cursors in my chart.

How can I use the left and right arrow keys to move the cursor selected?

I seen the demo doing it programmically, but I would like to do it with the keyboard.


Thanks,

Tom
Tom

Marjan
Site Admin
Site Admin
Posts: 745
Joined: Fri Nov 07, 2003 5:00 am
Location: Slovenia
Contact:

Post by Marjan » Wed Jan 31, 2007 7:13 am

Hi.

You can use the same code in the Chart OnKeyPress event. Something like this:

Code: Select all

procedure TForm1.Chart1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_LEFT then
    ChartTool1.XValue := ChartTool1.XValue - 1
  else if Key = VK_RIGHT then
    ChartTool1.XValue := ChartTool1.XValue + 1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues(100);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Chart1.SetFocus;
end;
Marjan Slatinek,
http://www.steema.com

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Wed Jan 31, 2007 12:06 pm

Thanks, that will work. Is it possible to capture what chart tool is clicked before doing this so I can move the one the user wants moved?


Thanks,

Tom
Tom

tbonejo
Newbie
Newbie
Posts: 73
Joined: Wed Sep 06, 2006 12:00 am
Contact:

Post by tbonejo » Wed Jan 31, 2007 12:27 pm

9242408 wrote:Thanks, that will work. Is it possible to capture what chart tool is clicked before doing this so I can move the one the user wants moved?


Thanks,

Tom
I got it. Just make MyCursorTool: TCursorTool and then on the change event assign MyCursorTool to whatever one it is.


Works nice.


Tom
Tom

Marjan
Site Admin
Site Admin
Posts: 745
Joined: Fri Nov 07, 2003 5:00 am
Location: Slovenia
Contact:

Post by Marjan » Wed Jan 31, 2007 12:36 pm

Hi, Tom.

Not directly. Ideally the tChart.CalcClickedPart should return clicked tool, but existing version does not support it. I'd use tChart OnMouseDown event and check if clicked position is over specific chart (cursor) tool. Here is the code I'd use:

Code: Select all

private
  SelectedCursor : TChartCursor;

procedure TForm1.Chart1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Assigned(SelectedCursor) then
  begin
    if KEY=VK_LEFT then SelectedCursor.XValue := SelectedCursor.XValue -1
    else if KEY=VK_RIGHT then SelectedCursor.XValue := SelectedCursor.XValue +1;
  end;
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var i: Integer;
begin
  SelectedCursor := nil;
  for i := 0 to Chart1.Tools.Count - 1 do
  begin
    if Chart1.Tools[i] is TCursorTool then
    With Chart1.Tools[i] as TCursorTool do
    begin
      if Clicked(X,Y)<>ccNone then
      SelectedCursor := Chart1.Tools[i] as TCursorTool;
      break;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SelectedCursor := nil;
end;
Marjan Slatinek,
http://www.steema.com

Post Reply