Reputation: 597
In my C# WinForms application, I'm using drag-and-drop to move items from a TreeView control to a Chart control. (It's a scheduling app with a list of jobs, and the user drops them onto the schedule). When the user drops an item onto an existing DataPoint on the chart, I want the new item to become a DataPoint and displace the old one (moving it down in the queue).
Here's what I have for the DragDrop event handler that's not quite (but almost) working:
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 anyone save my sanity and help me figure out how I can tell which DataPoint the user is dropping the job onto?
Upvotes: 3
Views: 1730
Reputation: 941317
The mouse position you get (e.X, e.Y) is in screen coordinates. You have to map it to the chart control. Fix:
var pos = chart1.PointToClient(new Point(e.X, e.Y));
HitTestResult testResult = chart1.HitTest(pos.X, pos.Y, ChartElementType.DataPoint);
Upvotes: 2