Page 1 of 1

Charttool TRectangleTool - delete it

Posted: Mon Apr 14, 2014 10:50 am
by 16467044
Hi,

I added several rectangles at runtime by the code below.
How can I address one rectangle at runtime to delete it?

To do it with
" ........ .DeleteSelected;"
would be great.

Thanks,
Cheryll

===========================

adding code:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  with Chart1.Tools.Add(TRectangleTool) as TRectangleTool do
  begin
    Text:='Rectangle Tool 1';
    AutoSize:=true;
    Shape.Transparency:=0;
    Left:=100;
    Top:=75;

    OnClick:=RectClick;
  end;
end;

procedure TForm1.RectClick(Sender:TAnnotationTool; Button:TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button=mbRight then
  begin
    with Chart1.Tools.Add(TRectangleTool) as TRectangleTool do
    begin
      Text:=Sender.Text + ' (twin)';
      AutoSize:=Sender.AutoSize;
      Shape.Transparency:=Sender.Shape.Transparency;
      Top:=Sender.Top+Sender.Height;
      Left:=Sender.Left;
      OnClick:=RectClick;
    end;
  end;
end;

Re: Charttool TRectangleTool - delete it

Posted: Tue Apr 15, 2014 10:38 am
by yeray
Hi Cheryll,

You could have a global variable (TRectangleTool) storing the selected tool. Then you could use event to change from a tool for another and to remove the selected one. In this example I change the rectangle color pen to highlight the selected tool, I use the same RectClick event to change the tool selected and I use the KeyDown event to remove the selected tool if Supr key is pressed.

Code: Select all

var selectedRect: TRectangleTool;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with Chart1.Tools.Add(TRectangleTool) as TRectangleTool do
  begin
    Text:='Rectangle Tool 1';
    AutoSize:=true;
    Shape.Transparency:=0;
    Left:=100;
    Top:=75;

    OnClick:=RectClick;
  end;
end;

procedure TForm1.RectClick(Sender:TAnnotationTool; Button:TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button=mbRight then
  begin
    with Chart1.Tools.Add(TRectangleTool) as TRectangleTool do
    begin
      Text:=Sender.Text + ' (twin)';
      AutoSize:=Sender.AutoSize;
      Shape.Transparency:=Sender.Shape.Transparency;
      Top:=Sender.Top+Sender.Height;
      Left:=Sender.Left;
      OnClick:=RectClick;
    end;
  end
  else if Button=mbLeft then
  begin
    if Assigned(selectedRect) then
       selectedRect.Shape.Pen.Color:=clBlack;

    selectedRect:=Sender as TRectangleTool;
    selectedRect.Shape.Pen.Color:=clRed;
  end;
end;

procedure TForm1.Chart1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_DELETE then
    if selectedRect<>nil then
    begin
      Chart1.Tools.Remove(selectedRect);
      Chart1.Repaint;
    end;
end;

Re: Charttool TRectangleTool - delete it

Posted: Mon Apr 28, 2014 11:17 am
by 16467044
Thank you so much!
This works fine.

Cheryll