WinForms chart: how can I define a DataPoint in a DragDrop event?

In my C # WinForms application, I use drag-and-drop to move items from a TreeView control to a Chart control. (This is a scheduling application with a list of tasks, and the user throws them into the schedule). When a user throws an element into an existing DataPoint in the diagram, I want the new element to become DataPoint and supplant the old one (moving it in the queue).

Here I have for the DragDrop event handler, which does not quite (but almost) work:

 private void chart1_DragDrop(object sender, DragEventArgs e)
 {
     if (draggedJob != null) // This is set when user starts dragging
     {
         HitTestResult testResult = chart1.HitTest(e.X, e.Y, ChartElementType.DataPoint);
         switch (testResult.ChartElementType)
         {
              case ChartElementType.DataPoint:
                  // This should happen if I dropped an item onto an existing DataPoint
                  // ...but testResult.ChartElementType is always "Nothing"
                  DataPoint existingPoint = (DataPoint)testResult.Object;
                  JobOrder jobToDisplace = (JobOrder)existingPoint.Tag;

                  ScheduleJob(draggedJob, jobToDisplace);
                  break;
              default:
                  //This happens every time (it adds the item to the end of the schedule)
                  ScheduleJob(draggedJob);
                  break;
         }                                        

         RefreshTreeView();
         RefreshChart();     

         draggedJob = null;
     }
 }

Can someone keep my sanity and help me figure out how I can determine which DataPoint user is quitting the job?

+3
source share
1 answer

, (e.X, e.Y), . . Fix:

var pos = chart1.PointToClient(new Point(e.X, e.Y));
HitTestResult testResult = chart1.HitTest(pos.X, pos.Y, ChartElementType.DataPoint);
+3

Source: https://habr.com/ru/post/1788337/


All Articles