【问题标题】:WinForms Chart: How can I identify a DataPoint within the DragDrop event?WinForms 图表:如何识别 DragDrop 事件中的数据点?
【发布时间】:2011-01-27 22:07:52
【问题描述】:

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

以下是我所拥有的 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 上?

【问题讨论】:

    标签: c# winforms drag-and-drop charts


    【解决方案1】:

    您获得的鼠标位置(e.X,e.Y)在屏幕坐标中。您必须将其映射到图表控件。修复:

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

    【讨论】:

    • 现在像冠军一样拖放。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多