Getting the Index of the Retangle Tool selected

TeeChart for ActiveX, COM and ASP
Post Reply
tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Getting the Index of the Retangle Tool selected

Post by tirby » Tue Mar 19, 2013 6:27 pm

Hi!

I've placed Select Case code in TChart1_OnRectangleToolDragging().
When the Retangle Tool is clicked on, how do I get the index of the Rectangle that has been selected?
In otherwords; How do I know which Rectangle the user has clicked on (and is now dragging)?

Thanks

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Getting the Index of the Retangle Tool selected

Post by tirby » Wed Mar 20, 2013 1:43 pm

Hello,

Between OnRectangleToolClick, OnRectangleToolDragging, and XY checking code, I was able to derive an index. This method is not terribly reliable, so I would still like a cleaner solution if there is one available!

Thanks

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Getting the Index of the Retangle Tool selected

Post by tirby » Wed Mar 20, 2013 6:22 pm

Ok, so this really isn't working well at all.
Please tell me there is an easy method of obtaining the ToolNumberIndex from a Rectangle that has been clicked!

Thanks!

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Getting the Index of the Retangle Tool selected

Post by Yeray » Thu Mar 21, 2013 12:47 pm

Hi tirby,

This simple example seems to work fine for me here:

Code: Select all

Private Sub Form_Load()
  TeeCommander1.ChartLink = TChart1.ChartLink
  'TChart1.Header.Text.Text = TChart1.Version
  Dim XValue, YValue As Double
  Dim XPos, YPos, tmpHeight As Integer
  Dim rect, dline As Long
  
  TChart1.Aspect.View3D = False
  
  TChart1.AddSeries scPoint
  TChart1.Series(0).FillSampleValues 10
  
  TChart1.Environment.InternalRepaint
  
  Dim i As Integer
  For i = 0 To TChart1.Series(0).Count - 1
    XPos = TChart1.Series(0).CalcXPos(i)
    YPos = TChart1.Series(0).CalcYPos(i)
    XValue = TChart1.Series(0).XValues.Value(i)
    YValue = TChart1.Series(0).YValues.Value(i)
  
    rect = TChart1.Tools.Add(tcRectangle)
    dline = TChart1.Tools.Add(tcDrawLine)
    With TChart1.Tools.Items(rect).asRectangle
      .Text = "Point " + Str$(i)
      .AutoSize = True
      tmpHeight = .Bounds.Bottom - .Bounds.Top
      .Left = XPos - (TChart1.Canvas.TextWidth(.Text) / 2)
      .Top = YPos - 10 - tmpHeight
    End With
    With TChart1.Tools.Items(dline).asDrawLine
      .EnableDraw = False
      .EnableSelect = False
      .AddLine XValue, YValue, XValue, TChart1.Axis.Left.CalcPosPoint(YPos - 10)
    End With
  Next i
End Sub

Private Sub TChart1_OnRectangleToolClick(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
  Dim i As Integer
  Dim clickedRect As Long
  
  clickedRect = -1
  For i = 0 To TChart1.Tools.Count - 1
    If TChart1.Tools.Items(i).ToolType = tcRectangle And TChart1.Tools.Items(i).asRectangle.Clicked(X, Y) Then
      clickedRect = i
      Exit For
    End If
  Next i
  
  If clickedRect > -1 Then
    MsgBox "Rectangle clicked. Tool index: " + Str$(clickedRect)
  End If
End Sub
If you still find problems with it, please try to arrange a simple example project we can run as-is to reproduce the problem here. This usually helps customers to find a fix for themselves.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Getting the Index of the Retangle Tool selected

Post by tirby » Thu Apr 25, 2013 4:03 pm

Ok, I'm finally back on this project!

The problem with the code that Yeray posted on 21-Mar, is that it does not allow for a situation where multiple rectangles are stacked on top of one another.
If rectangle #2 is placed over rectangle #1, partially covering rectangle #1, then when the user clicks on rectangle #2 in the area where rectangle #1 is directly beneath rectangle #2, the routine will find the cursor within the bounds of rectangle #1 and return the index for rectangle #1.
How can I get this to work correctly?

These tools really need some sort of index property.

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Getting the Index of the Retangle Tool selected

Post by Yeray » Fri Apr 26, 2013 2:18 pm

Hello,
tirby wrote:How can I get this to work correctly?
Correct here is something quite subjective. I mean, having two rectangles superposed, and clicking in the region shared by both rectangles, both answers would be correct because actually both rectangles are under the mouse when the click if made.
So, you are who has to decide what to return in these cases. How? We are looping from index 0 to Count-1 in OnRectangleToolClick event. That's why we get the first tool that is being clicked. If you want the second, you can loop reverse, from Count-1 to 0.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Getting the Index of the Retangle Tool selected

Post by tirby » Fri Apr 26, 2013 2:21 pm

Point taken.

Thanks Yeray!

Post Reply