Nevron Forum

get drag and drop events of a shape working

https://www.nevron.com/Forum/Topic5377.aspx

By sandro schimke - Friday, July 15, 2011

Hello,

I'd like to drag a shape and drop it somewhere else on a grid. In the moment I drop it, I'd like to check if it's permitted to drop it there. I think I should use the MouseUp event of the shape in the preview layer. Preview shape and original shape have the same uniqueID, that's how I get the original coordinates. But I don't know how to set the event for the preview shape.

NDrawingView1.PreviewLayer.FireEvents = True
NDrawingDocument1.EventSinkService.Start()
AddHandler NDrawingDocument1.EventSinkService.NodeMouseUp, AddressOf Me.handleMouseUp

With that code I receive events not only for the shapes, but for all MouseUps on the grid.

So, how can I access the preview shape to assign an eventhandler?
Is it actually the right way? DragEnter and DragLeave events of the original shape themself, as I understood, are used for a different challenge.
By Nevron Support - Friday, July 15, 2011

Hi Sandro,

The best way to forbid a shape from dropping on a specific area of the grid is to use the NodeBoundsChanging event of the drawing document’s event sink service:

 

document.EventSinkService.NodeBoundsChanging += new NodeBoundsCancelEventHandler(OnNodeBoundsChanging); 

 

The event is cancelable, so you just have to assign true to the Cancel property of the supplied event args to cancel the shape to move to its new location. Here’s a simple example for the NodeBoundsChanging event handler:

 

 private void OnNodeBoundsChanging(NNodeBoundsCancelEventArgs args)

{

      // The following will be an example area that is not allowed for shapes

      NRectangleF forbiddenArea = new NRectangleF(100, 100, 300, 300);

 

      // Get the shape which bounds has changed

      NShape shape = args.Node as NShape;

      if (shape == null)

            return;

 

      // Check if the shape is dropped on an allowed location

      NRectangleF bounds = args.NewBounds;

      if (forbiddenArea.IntersectsWith(bounds))

      {

            // Cancel the the dragging of the shape

            args.Cancel = true;

      }

}