繁体   English   中英

WinForms图表:如何在DragDrop事件中识别DataPoint?

[英]WinForms Chart: How can I identify a DataPoint within the DragDrop event?

在我的C#WinForms应用程序中,我使用拖放操作将项目从TreeView控件移动到Chart控件。 (这是一个带有作业列表的调度应用程序,用户将它们放入计划中)。 当用户将项目放到图表上的现有DataPoint上时,我希望新项目成为DataPoint并替换旧项目(在队列中向下移动)。

这是我对DragDrop事件处理程序的不完全(但几乎)工作:

 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;
     }
 }

任何人都可以保存我的理智并帮助我弄清楚我如何判断用户正在将作业放到哪个DataPoint上?

您获得的鼠标位置(eX,eY)位于屏幕坐标中。 您必须将其映射到图表控件。 固定:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM