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
Move Cursor Tool with arrow keys
Hi.
You can use the same code in the Chart OnKeyPress event. Something like this:
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
http://www.steema.com
I got it. Just make MyCursorTool: TCursorTool and then on the change event assign MyCursorTool to whatever one it is.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
Works nice.
Tom
Tom
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:
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
http://www.steema.com